首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何修复这个无效的update: Invalid How How section 0错误?

如何修复这个无效的update: Invalid How How section 0错误?
EN

Stack Overflow用户
提问于 2019-06-20 03:35:14
回答 3查看 567关注 0票数 0

我正在使用Swift 5中的UITableView进行实验。

我想插入和删除行。

我的表在添加额外的行时崩溃。第一次插入工作正常。

代码语言:javascript
复制
class ViewController: UITableViewController {

    var items = [Int]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.backgroundColor = .white
        tableView.tableFooterView = UIView()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "CELL_ID")

        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.addMoreRows([1,2,3,4,5])
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            self.addMoreRows([6,7,8,9,10])
        }

    }

    private func addMoreRows(_ data: [Int]) {

        self.items.append(contentsOf: data)

        var indexPaths = [IndexPath]()

        for i in 0...items.count - 1 {
            indexPaths.append(IndexPath(row: i, section: 0))
        }

        tableView.insertRows(at: indexPaths, with: .left)
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CELL_ID", for: indexPath)
        cell.textLabel?.text = "Cell #\(indexPath.row)"
        return cell
    }
}

由于未捕获的异常'NSInternalInconsistencyException',

正在终止app,原因:‘无效更新:节0中的行数无效。更新后的现有节中包含的行数(10)必须等于更新前该节中包含的行数(5),加上或减去从该节中插入或删除的行数(插入10行,删除0行),再加上或减去移入或移出该节的行数(0移入,0移出)。’

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56674705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档