我正在使用Swift 5中的UITableView
进行实验。
我想插入和删除行。
我的表在添加额外的行时崩溃。第一次插入工作正常。
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移出)。’
https://stackoverflow.com/questions/56674705
复制相似问题