【发布时间】:2018-10-10 15:17:54
【问题描述】:
我在 TableView 中插入单元格时遇到崩溃。我试过下面的链接,但不确定是什么问题
下面是我的代码
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = names[indexPath.row]
return cell
}
数据源代码如下
private var names: [String] = (50...99).map { String($0) }
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
self.appendCells()
}
}
private func appendCells() {
names = (0...49).map { String($0) } + names
customTableView.beginUpdates()
let indexPat = IndexPath(row: names.count - 1, section: 0)
customTableView.insertRows(at: [indexPat], with: .fade)
customTableView.endUpdates()
}
我无法理解我在这里缺少什么。对不起,如果我做傻事。如果我需要解释更多,请告诉我。
我收到错误:
原因:'无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (100) 必须等于更新前该节中包含的行数(50),
【问题讨论】:
-
与往常一样,
beginUpdates / endUpdates对于单个insert/delete/move操作毫无意义。
标签: swift uitableview