How to reload table view after row reordering? Rows have variable height

I allow the user to reorder the rows in the tableView . Since this event affects the content โ€” some numeric values โ€‹โ€‹in the cells must be updated โ€” on all other lines, I call reloadData on moveRowAtIndexPath . And then strange effects happen.

those. The cells appear to overlap when the dragger is touched, and some cells begin to move up and down. It is important to know that the height of the cells is different.

It is strange that if I remove reloadData from moveRowAtIndexPath , then all these phenomena will disappear. Only content is not allowed.

So, how do I reload data after reordering?


UPDATE: In the meantime, I reconfigured the cells in viewDidLayoutSubviews instead of calling reloadData end of moveRowAtIndexPath . And it works 90%, as I expect, but still the lines are sometimes slightly higher than they should.

 override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { //.. reorderOccured = true } override func viewDidLayoutSubviews() { if reorderOccured { for cell in tableView.visibleCells() as! [UITableViewCell] { let ip = tableView.indexPathForCell(cell) if ip != nil { self.configureCell(cell, indexPath: ip!) } } reorderOccured = false } } 
+7
ios uitableview
source share
3 answers

I found the answer here: fooobar.com/questions/98471 / ...

 [tableView beginUpdates]; [tableView endUpdates]; 

This code causes the UITableView to update cell sizes only, but not the contents of the cell.

+3
source share

You should not call reloadData after changing the order. You must make the same changes to your data as the changes made on the screen. For example: if you moved cell No. 2 to position 6, you need to delete your object, which fills cell No. 2, and insert it again at position 6. You did not specify enough details, but usually you will store your data in an array of objects. This array needs to make a change, so your backup data source is valid.

Here is a link for details from Apple.

I just read the update after I posted my answer. It seems you really need reloadData . In this case, I recommend rebooting after a short delay using the dispatch_async block on the main thread. Say after 0.1.

+2
source share

I cannot comment on pteofil's answer yet, but it is right: if you have a numbered set of rows in a table and you move one call to moveRow (...), your animation for moving will be canceled using tableView.reloadData ().

So I delayed reloading the data (which renumbers all visible cells based on the updated data source (don't forget to do this when you move things!)) A couple of hundred milliseconds and it works fine now and looks great too.

0
source share

All Articles