UITableViewCell一些基本用法

UITableViewCell一些基本用法_第1张图片
屏幕快照 2016-11-29 下午4.51.21.png

# UITableViewCell一些基本用法
一、
1:UITableViewController继承自UIViewController,自带一个tableview
2:self.view不是UIView而是UITableView
3:datasource和delegate默认都是self(UITableViewController)
4:开发中只需要建立UITableViewController子类。就会帮助我们实现datasource和delegate协议中的一些相关的方法,比如tableview编辑(增加,删除,移动),以及多少的分区,每个分区有多少个cell和返回cell视图的方法,当你需要的时候只需要打开相应的注视即可。
二、
tableView编辑的步骤:
1.让tableView出于编辑状态
2.设置哪些cell可以编辑
3.设置编辑的样式(删除,插入)
4.提交编辑操作(1.先修改数据源,2.更新UI)
三、
tableView移动的步骤:
1.让tableView出于编辑状态
2.设置哪些cell可以编辑
3.提交移动结果(1.只需要更新数据源即可)

 //重用标识
    let identifier = "cell"
    
    //联系人字典属性 var dic:[String:[String]]
    var contactSource = [
        "W":["王哲磊","王浩","王乐","王晨阳"],
        "C":["陈扬","陈芮"],
        "B":["边文达","宝音","白婧"],
        "L":["李玲","刘二蛋","刘婧","刘福宏"]
    ]
    

    //存放排好序的key值
    var keysArray:[String]?
override func viewDidLoad() {
        super.viewDidLoad()

        //取出字典contactArray中的key值
        let keys = self.contactSource.keys
        //排序后赋值给keysArray
        keysArray = keys.sorted()
       //print(keysArray)
        
        //注册cell
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
        
        //1.让tableView出于编辑状态
         self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

此方法必须打开

override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

返回分区个数

 override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        //分组的个数 = 字典中键值对的个数
        //分组的个数 = 存放排好序key值数组元素个数
        return contactSource.count
    }

返回cell个数

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // 先取出排好序key值数组的对应分区的key值
        let key = keysArray?[section]
        
        let group = contactSource[key!]
        
        return (group?.count)!
    }

返回cell

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)

      //取出字典中key值对应数组元素的值赋值给textLabel
        let key = keysArray?[indexPath.section]
        //根据key值取出字典中的value值
        let group = contactSource[key!]
        //根据cell的下标取出数组中对应位置的元素
        let name = group?[indexPath.row]
        cell.textLabel?.text = name

        return cell
    }

区头标题

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
     
        return "\(keysArray![section])"
    }

索引列表

override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return keysArray
    }

设置哪些cell可以编辑

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        
        if indexPath.section < 2{
           return true
        }
        return false
    }

设置编辑的样式(删除,插入)

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        if indexPath.section == 0 {
            return .delete
        }else if indexPath.section == 1 {
            return .insert
        }
        return .none
        
    }

提交编辑操作(1.先修改数据源,2.更新UI

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
       
        //先取出选中cell所在的分区对应的key值
        let key = keysArray?[indexPath.section]
        //根据key值取出对应的value值数组
        var group = contactSource[key!]
        
        if editingStyle == .delete {
           
            //删除整个分区
            if group?.count == 1{
               //1.删除字典中的键值对
                contactSource.removeValue(forKey: key!)
                //2.删除排好序的keysArray数组中的元素
                keysArray?.remove(at: indexPath.section)
                //3.更新UI
                let set = NSIndexSet(index: indexPath.section)
                tableView.deleteSections(set as IndexSet, with: .left)
                
            }else{//一条条的删除cell
                //先修改数据源
                group?.remove(at: indexPath.row)
                //删除之后重新为字典中values赋值(group删除并没有将字典中的数组内容删除)
                contactSource[key!] = group
                //更新UI
                tableView.deleteRows(at: [indexPath], with: .fade)
            }
            
        } else if editingStyle == .insert {
            
            //准备要插入的数据
            let name = "逗逼"
            //1.先修改数据源
            group?.append(name)
            //2.重新对字典的键值对赋值
            contactSource[key!] = group
            //3.更新UI
            tableView.insertRows(at: [indexPath], with: .right)
            //4.让tableView重新加载数据
            tableView.reloadData()
        }    
    }

删除按钮的文字

override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "删除"
    }

设置哪些cell可以移动

override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
      return true
    }

提交移动结果(只需要更新数据源即可)

override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

        //修改数据源
        let key = keysArray?[fromIndexPath.section]
        var group = contactSource[key!]
        //取出数组中原位置的元素
        let name = group?[fromIndexPath.row]
        //删除数组中原来位置的元素
        group?.remove(at: fromIndexPath.row)
        //插入数组中新的位置
        group?.insert(name!, at: to.row)
        //重新对字典中key值对应的value值赋值
        contactSource[key!] = group
    }

/// Description:限制跨分区移动
///
/// - Parameters:参数
/// - tableView: tableView对象,代理的委托人
/// - sourceIndexPath: 移动之前cell的位置
/// - proposedDestinationIndexPath: 移动之后cell的位置
/// - Returns: cell移动之后最终的位置

override func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
        //根据分区的下标判断是否允许移动,当前后的位置在同一个分区,允许移动返回移动之后的位置。当前后的位置不在同一个分区,允许移动返回移动之前的位置。
        if sourceIndexPath.section == proposedDestinationIndexPath.section {
            return proposedDestinationIndexPath
        }else{
            return sourceIndexPath
        }
    }

你可能感兴趣的:(UITableViewCell一些基本用法)