2017-08-26 90 views
-2

我以前使用下面的代码来实现删除了细胞在我UITableView冲突的UITableView编辑功能

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 
     feedTable.deleteRows(at: [indexPath!], with: .fade) 
    } 
} 

不过,现在我想添加自定义操作,所以我把这个添加的细胞:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let edit = UITableViewRowAction(style: .normal, title: "Edit", handler: { (action, indexPath) in 
    }) 
    let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in 
    } 
    edit.backgroundColor = UIColor.green 
    return [delete, edit] 
} 

现在我很困惑我是否需要连原commit editingStyle功能。我是否应该将所有编辑处理代码(feedTable.deleteRows(at: [indexPath!], with: .fade))移至新功能?

在我看来,有很多不同的功能与UITableViewCell编辑有关,我很困惑要使用哪些功能。

回答

0

我是否应该将所有编辑处理代码(feedTable.deleteRows(at:[indexPath!],with:.fade))移动到新函数中?

的回答你的问题:是

当你执行的tableView(_:editActionsForRowAtIndexPath:)方法,表视图将不再为您生成的删除按钮。这就是为什么你需要创建你自己的删除按钮。

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 

    let edit = UITableViewRowAction(style: .normal, title: "Edit", handler: { (action, indexPath) in 
     // your handling code 
    }) 

    let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in 
     // your handling code 
    } 

    // buttons colors 
    edit.backgroundColor = UIColor.green 
    delete.backgroundColor = UIColor.red 

    return [delete, edit] 
}