UITableView插入行装饰



我正在尝试通过在导航栏上提供"编辑"按钮来启用对UITableView的编辑。

其目的是允许用户插入新项目和删除现有项目。当点击编辑按钮时,iOS显示"删除"装饰(删除效果良好(,但不显示"插入">

我已将navigationItem.rightBarButtonItem = editButtonItem添加到我的UIViewControllers viewDidLoad中,并将以下方法添加到UITableViewDataSource委托中:

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
AppDelegate.persistenceContext.delete(self.fetchedResultsController.object(at: indexPath))
AppDelegate.saveContext()
} else if editingStyle == .insert {
let current = self.fetchedResultsController.object(at: indexPath)
// do rest of insertion logic
}
}

我错过了什么?

哦,出于兴趣,如果可能的话,新的单元格会被插入到启动操作的单元格的上方或下方吗?

记录在案,我可以在单元格中添加自定义滑动操作,但用户的反馈是他们更喜欢编辑按钮。

任何光线都将不胜感激,谷歌搜索给我带来的只是使用按钮插入新行,或添加自定义滑动动作。

您需要实现委托方法:

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle

返回给定索引路径的.delete.insert.none。通常只有一行(通常是第一行或最后一行(将返回.insert,其余的将返回.delete。对于任何无法插入或删除的行,返回.none

为了显示绿色+(.insert(图标,您还需要重写setEditing(_ editing: Bool, animated: Bool)方法来添加/删除额外的行,这并不罕见。

另一种选择是在导航栏中显示+图标,而不是带有插入图标的行。

最新更新