【问题标题】:How to hide section in tableView after deleting last item?删除最后一项后如何隐藏 tableView 中的部分?
【发布时间】:2019-12-11 01:06:45
【问题描述】:

当我删除 section 中的最后一项时,此部分中的页脚可见。我怎么能隐藏它? 我用来删除tableView.deleteRows(at: [indexPath], with: .fade),而不是tableView.reloadData()

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        guard editingStyle == .delete else { return }
        guard let section = ComistType.init(rawValue: indexPath.section) else { return }

        taskList[section]?.remove(at: indexPath.item)
        if taskList[section]?.count == 0 {
            taskList.removeValue(forKey: section)

        }
    tableView.deleteRows(at: [indexPath], with: .fade)
}

enter image description here

'NSInternalInconsistencyException',原因:'无效更新:无效 节数。表格视图中包含的节数 更新后 (2) 必须等于所包含的节数 在更新前的表视图中(3),加减数 插入或删除的部分(0 插入,2 删除)。'

【问题讨论】:

  • 你有没有试过在需要的时候使用tableView.deleteSections(),这样就没有'空白部分'了?
  • 我有一个错误。查看堆栈跟踪上层

标签: ios swift uitableview uikit


【解决方案1】:

正如 Chris Shaw 提议的,你可以使用UITableViewdeleteSections(_:with:) 函数。

例如:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    guard editingStyle == .delete else { return }
    guard let section = ComistType.init(rawValue: indexPath.section) else { return }

    taskList[section]?.remove(at: indexPath.item)
    if taskList[section]?.count == 0 {
        taskList.removeValue(forKey: section)

        // This line does the magic
        tableView.deleteSections([section], with: .fade)
    }
    tableView.deleteRows(at: [indexPath], with: .fade)
}

查看Apple documentation了解更多详情。

更新

不要忘记在 tableview 数据源中反映部分的总数 使用numberOfSections(in:)委托函数。

【讨论】:

  • 我知道这个方法,但是当我调用它时 - 有一个错误。看问题,我添加了错误堆栈跟踪
  • 我更新了答案。您需要在 numberOfSections(in:) 委托方法中返回正确数量的部分
【解决方案2】:

删除部分后,您的 numberOfRowsInSection 方法会为其他部分返回错误值。

假设您有三个部分:ComistType(rawValue: 0)ComistType(rawValue: 1)ComistType(rawValue: 2)。然后,如果您删除带有ComistType(rawValue: 1) 的部分,那么您的字典现在有两个键:ComistType(rawValue: 0)ComistType(rawValue: 2)。它应该有 ComistType(rawValue: 0)ComistType(rawValue: 1) 以使表格视图在删除后正常工作。

最好使用数组数组,这样您就不必更新所有键。

试试这个:

var taskList: [[Int]] = [[1, 2], [4, 5]]

override func numberOfSections(in tableView: UITableView) -> Int {
    return taskList.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return taskList[section].count
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    guard editingStyle == .delete else { return }

    taskList[indexPath.section].remove(at: indexPath.item)
    if taskList[indexPath.section].count == 0 {
        taskList.remove(at: indexPath.section)
        tableView.deleteSections([indexPath.section], with: .fade)
    } else {
        tableView.deleteRows(at: [indexPath], with: .fade)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 2015-07-06
    • 1970-01-01
    相关资源
    最近更新 更多