简体   繁体   中英

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. 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:

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.

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. I would suggest cell.titleLabel!.text either way since that is the data you are seeing repeated.

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

  4. Try reloading the data immediately after a delete with the reloadData() method for UITableView.

See the UITableView reference document.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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