简体   繁体   English

表格单元格无法从TableView Swift中正确删除

[英]Table cells not deleting properly from tableView swift

I am using a function other than apple's provided methods ( canEditRowAtIndexPath and commitEditingStyle ) to delete cells from a tableView. 我使用的不是Apple提供的方法( canEditRowAtIndexPathcommitEditingStyle ), canEditRowAtIndexPathcanEditRowAtIndexPath删除单元格。 This is my code: 这是我的代码:

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

it does everything which generally occurs when deleting the rows. 它执行删除行时通常发生的所有操作。 However, when new rows are inserted into the tableView, they layer the data from one of the previously deleted cells on top of the new ones, in this way: 但是,当将新行插入到tableView中时,它们会通过以下方式将来自先前删除的单元格之一的数据分层放置在新单元格之上:

The items can be added easily, 这些项目可以轻松添加,

第一

They can be deleted easily as well, 它们也可以轻松删除,

第二

but when you try to add more cells, it has a problem: 但是,当您尝试添加更多单元格时,就会出现问题: 在此处输入图片说明

At the moment, my best guess is that it has a problem with deletion of cells. 目前,我最好的猜测是它存在细胞缺失的问题。 any advice would be greatly appreciated. 任何建议将不胜感激。

At the moment, my best guess is that it has a problem with deletion of cells. 目前,我最好的猜测是它存在细胞缺失的问题。 any advice would be greatly appreciated. 任何建议将不胜感激。

If you can confirm for sure that your delete method is being called, by either using the debugger or a print statement, then I would say that your cells contain stale data from being dequeued, this would align with the laying you are seeing. 如果您可以通过使用调试器或print语句来确定您的delete方法正在被调用,那么我想说您的单元格包含过时数据,不会被出队,这将与您所看到的布局保持一致。

How do we confirm this? 我们如何确认这一点?

  1. Check your logic for your 'Add New Item' functionality. 检查“添加新项”功能的逻辑。

  2. Check your data source and make sure that it contains the correct data and number of items, you could check this in your custom delete method. 检查您的数据源,并确保它包含正确的数据和项目数,您可以在自定义删除方法中进行检查。

  3. Set a breakpoint in the UITableViewDatasourceDelegate method below and inspect your cell's properties or use print statements to investigate. 在下面的UITableViewDatasourceDelegate方法中设置一个断点,并检查单元格的属性或使用print语句进行调查。 I would suggest cell.titleLabel!.text either way since that is the data you are seeing repeated. 我建议以任何一种方式推荐cell.titleLabel!.text,因为那是您看到的重复数据。

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

  4. Try reloading the data immediately after a delete with the reloadData() method for UITableView. 尝试使用UITableView的reloadData()方法删除后立即尝试重新加载数据。

See the UITableView reference document. 请参阅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. 讨论出于性能原因,表视图的数据源通常在为其表tableView:cellForRowAtIndexPath:方法中的行分配单元格时应重用UITableViewCell对象。 A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. 表视图维护数据源已标记为可重复使用的UITableViewCell对象的队列或列表。 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. 如果一个可用的单元格可用,则此方法使该单元出队,或者使用您先前注册的类或nib文件创建一个新的单元格。 If no cell is available for reuse and you did not register a class or nib file, this method returns nil. 如果没有可供重用的单元格,并且您没有注册类或nib文件,则此方法返回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. 如果您为指定的标识符注册了一个类,并且必须创建一个新的单元格,则此方法通过调用其initWithStyle:reuseIdentifier:方法来初始化该单元格。 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. 如果现有单元可供重用,则此方法改为调用单元的prepareForReuse方法。

I just had this problem. 我只是有这个问题。 You can't just have 你不能只是拥有

self.tableView.deleteRows(at: [indexPath], with: .fade)

You have to remove it from your array too like this: 您也必须像这样从阵列中删除它:

myArray.remove(at: indexPath.row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM