首页 > 解决方案 > 从具有多个部分的 tableview 中删除

问题描述

我正在尝试删除UITableView包含多个部分的行。当有 1 个部分时,我看到的所有教程都会删除一行。

我的数据 [[String]]:

[["Section 0, Item 0", "Section 0, Item 1"], ["Section 1, Item 0"]]

无论我刷哪一行,我都会收到错误消息:

'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

我的删除代码:

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let pulled = UIContextualAction(style: .destructive, title: "Pulled", handler: {(_, _, completionHandler) in
        self.pullList.remove(at: indexPath.row)
        self.tableView.deleteRows(at: [indexPath], with: .fade)
        
        completionHandler(true)
    })
    
    return UISwipeActionsConfiguration(actions: [pulled])
}

如果有专门针对此的教程,那就太棒了。我的部分可以有 1-4 行。

标签: swifttableviewdelete-row

解决方案


如果pullList[[String]]你必须删除

self.pullList[indexPath.section].remove(at: indexPath.row)

推荐阅读