Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload tableview after reordering a row? Rows have variable height

I let user to reorder rows in tableView. Because this event affects content - some numeric values in cells should be updated - in all the other rows, I call a reloadData in moveRowAtIndexPath. And then strange effects occur.

I.e. cells seems overlapping when touching dragger, and some cell starts to move up and down. Its important to know, that cells height are varying.

The strange, that if I remove reloadData from moveRowAtIndexPath, then all these phenomenons disappear. Only the content is invalid.

So how should I reload data after reordering?


UPDATE: What I have done meantime reconfiguring cells in viewDidLayoutSubviews instead of call reloadData end of the moveRowAtIndexPath. And it works 90% like I expect, but still rows are sometimes somewhat higher 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
    }
}
like image 443
János Avatar asked Jun 09 '15 15:06

János


1 Answers

You shouldn't call reloadData after reorder. You have to make the same changes to your data, as the changes that has been made on the screen. For example: if you moved cell nr 2 to position 6, you have to remove your object that populate cell nr.2 and insert it again at position 6. You didn't provide enough details, but usually you would keep your data in an array of objects. This array you have to make the changes to, so your backing datasource is valid.

Here's a link to details from Apple.

I just read the updated after I posted my answer. It seems like you really need to reloadData. In this case I advise to reload after a small delay with a dispatch_async block on the main thread. Say after 0.1.

like image 60
pteofil Avatar answered Sep 22 '22 02:09

pteofil