gpt4 book ai didi

ios - 表格单元格无法从 tableView swift 中正确删除

转载 作者:行者123 更新时间:2023-11-29 01:23:46 25 4
gpt4 key购买 nike

我正在使用 apple 提供的方法(canEditRowAtIndexPathcommitEditingStyle)以外的函数从 tableView 中删除单元格。这是我的代码:

func deleteItem() {
items.removeAtIndex(currentIndexPath!.row)
tableView.deleteRowsAtIndexPaths([currentIndexPath!], withRowAnimation: .Automatic)
}

它会执行删除行时通常发生的所有操作。但是,当新行插入到 tableView 时,它们会将先前删除的单元格之一的数据分层放置在新单元格之上,以这种方式:

可以轻松添加项目,

first

它们也可以很容易地删除,

second

但是当你尝试添加更多单元格时,就会出现问题: enter image description here

目前,我最好的猜测是它存在删除单元格的问题。任何建议将不胜感激。

最佳答案

目前,我最好的猜测是单元格删除有问题。任何建议将不胜感激。

如果您可以通过使用调试器或打印语句确认您的删除方法正在被调用,那么我会说您的单元格包含因出列而导致的过时数据,这将与您所看到的布局一致.

我们如何确认这一点?

  1. 检查“添加新项目”功能的逻辑。

  2. 检查您的数据源并确保它包含正确的数据和项目数量,您可以在自定义删除方法中检查这一点。

  3. UITableViewDatasourceDelegate 中设置断点下面的方法并检查您的单元格的属性或使用打印语句进行调查。无论哪种方式,我都建议使用 cell.titleLabel!.text,因为这是您看到的重复数据。

    tableView(_ tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath)

  4. 尝试在使用 reloadData() 删除后立即重新加载数据UITableView 的方法。

参见 UITableView引用文档。

Discussion For performance reasons, a table view’s data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. If no cell is available for reuse and you did not register a class or nib file, this method returns nil.

If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.

关于ios - 表格单元格无法从 tableView swift 中正确删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34274673/

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