美文网首页
IOS:Swift:UITableView的编辑操作

IOS:Swift:UITableView的编辑操作

作者: 任任任任师艳 | 来源:发表于2016-11-29 18:04 被阅读0次

AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
self.window?.makeKeyAndVisible()
//创建导航视图控制器的根视图
let vc = RootTableViewController()
//2.创建导航视图控制器,并为她制定根视图控制器
let navigation = UINavigationController(rootViewController: vc)

    //3.将导航视图控制器设置为window的根视图控制器
    self.window?.rootViewController = navigation

    
    return true
}

RootTableViewController.swift
//不需要手动遵循协议和实现方法 RootTableViewController
/*
一、UITableViewController继承自UIViewController,自带一个tableview
二、self.view不是UIView而是UITableView
三、DataSource和delegate默认都是self(UITableViewController)
四、开发中只需要建立UITableViewController子类,就会帮我们实现datasource和delegate协议中一些相关方法,比如tableView编辑(增加/删除、移动),以及多少分区,每个分区有多少cell和返回cell视图的方法,当你需要的时候只需要打开相应的注释即可
/
/

tableView编辑的步骤:
1.让tableView处于编辑状态
2.协议设定:
2.1设置那些cell可以编辑
2.2设置编辑样式(插入删除)
2.3提交编辑操作(1.先修改数据源2.更新UI)
/
/

tableView移动的步骤
1.让tableView处于编辑状态
2.设置哪些cell可以移动
3.提交移动结果(1.只需要更新数据源即可)
*/
class RootTableViewController: UITableViewController {

//        联系人字典属性    var dic:[String:[String]]  字典类型
var contactsource = [
    "w":["王哲磊","王浩","王乐","王晨阳"],"c":["陈扬","陈芮"],"b":["边文达","宝音","白婧"],"l":["刘二蛋","刘婧","刘福宏","李玲"]]
//存放排好序的key值
var keysArray:[String]?
//重用标识
let identifier = "cell"
override func viewDidLoad() {
    super.viewDidLoad()
    
    //取出字典contactsource中的key
    let keys = self.contactsource.keys
    //,排序后赋值给keyArray
    keysArray = keys.sorted()

// print(keysArray!)

    //注册限cell
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
    
    
    
    
    
     self.navigationItem.rightBarButtonItem = self.editButtonItem
    
    
    
}
  override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        //分组个数=字典中键值对的个数
        //分组个数=存放排好序key值数组元素个数
        return contactsource.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        //先取出排好序key值数组中对应分区的key值
        let key = keysArray?[section]
        let group = contactsource[key!]
        
        return (group?.count)!
    }

   
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
//取出字典中key值对应数组元素的值赋值给textLabel
        //根据分区的下标取出key
        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
    }
 /* 2.设置哪些cell可以编辑*/
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        if indexPath.section < 2{
            return true
        }
        return false
    }
   //3。设置编辑样式(插入/删除)
    override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        if indexPath.section == 0{
        return .delete
        }else if indexPath.section == 1{
        return .insert
        }
        return .none
    }
    /*4.提交编辑操作*/
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        //先取出选中cell所在的分区对应的key值
        let key = keysArray?[indexPath.section]
        //根据key值取出对应的value值数组
        var group = contactsource[key!]
//        print(group)
        
        if editingStyle == .delete {
            if group?.count == 1{
            //删除整个分区
                //删除字典中的键值对
                contactsource.removeValue(forKey: key!)
               //根据key值删除索引栏   删除排好序的
                keysArray?.remove(at: indexPath.section)
                //更新UI
                let set = NSIndexSet(index: indexPath.section)
                tableView.deleteSections(set as IndexSet, with: .left)
                
            }else{
                //一条一条的删除cell
                //1.修改数据源
                group?.remove(at: indexPath.row)
                //          print(group)
                //            print(contactsource[key!])
                //删除之后重新为key对应的value赋值
                contactsource[key!] = group
                
                //2.更新UI
                tableView.deleteRows(at: [indexPath], with: .fade)

            
            }
        
        
        } else if editingStyle == .insert {
            //准备要插入的数据
            let name = "小太阳"
            //先修改数据源
            group?.append(name)
            //重新为字典contactsource的键值对赋值
            contactsource[key!] = group
            //更新UI
            tableView.insertRows(at: [indexPath], with: .right)
            //4.让tableView重新加载数据
            tableView.reloadData()

            
            
        }    
    }
    //2.设置哪些cell可以移动
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    
    
    /*3.提交移动的结果  */
     // Override to support rearranging the table view.
     override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
     //修改数据源
        //g
        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
     }

    
    //限制跨分区移动
    /*sourceIndexPath移动之前cell的位置
     proposedDestinationIndexPath移动之后cell的位置
     command + option + /
     */
    /// 限制cell跨分区移动
    ///
    /// - 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
        }
    }
    

相关文章

网友评论

      本文标题:IOS:Swift:UITableView的编辑操作

      本文链接:https://www.haomeiwen.com/subject/aiqzpttx.html