gpt4 book ai didi

ios - UITableView insertRows(位于: indexPath) returns error of invalid number of rows

转载 作者:行者123 更新时间:2023-11-29 05:15:19 25 4
gpt4 key购买 nike

我有一个 tableView ,它应该显示图像数组。该数组之前已填充了 5 个项目,我需要使用 insertRows 来填充我的 tableView。问题是我收到以下错误:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

我的代码如下:

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

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ImagesTableViewCell
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
cell.cellImageView?.image = UIImage(data: images[indexPath.row]!)
tableView.endUpdates()
}

非常感谢您提供解决此问题的任何线索。

最佳答案

您的 cellForRowAtIndexPath 应仅包含这么多代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ImagesTableViewCell
cell.cellImageView?.image = UIImage(data: images[indexPath.row]!)
}

从 cellForRowAt 中删除 insertRows,您的代码将正常工作。

当我们想要向 tableView 添加新元素时,我们调用 Insert rows 函数。您的代码中不需要它,因为您已经有预定义的图像数组。

关于ios - UITableView insertRows(位于: indexPath) returns error of invalid number of rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59247718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com