Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload data on UITableView that is not being displayed

In iOS, are there any potential pitfalls to reloading a UITableView that is not currently being displayed?

I have found that I had some very peculiar bugs where cell content was disappearing but it only seemed to happen when I had switched to another Tab.. (the UIViewController with tableView on the first tab is still in memory and responding to external API updates)

so the line:

dispatch_async(dispatch_get_main_queue(), {

    self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: 0)], withRowAnimation: UITableViewRowAnimation.None)

}

is executing code to modify the display of a table that is not actually being displayed and this is when the bug was happening.

if I added some code to the viewControllers ViewDidAppear and ViewDidDisappear to basically block updates to the tableView (and then do a full refresh when re-entering that viewController) it seemed to solve the problem.

dispatch_async(dispatch_get_main_queue(), {

    if self.delegate.viewIsDisplayed() {
       self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: 0)], withRowAnimation: UITableViewRowAnimation.None)
    }

}

I do not know why the problem was happening and was hoping someone might be able to shine some light on what I was observing before (cells randomly clearing content and then re-appearing on each subsequent reload)

like image 228
Md1079 Avatar asked Oct 30 '25 02:10

Md1079


1 Answers

reloadRowsAtIndexPaths() always attempts some form of animation, .None just tells it to use the default animations (see docs). This might be messing with your cell's content of the tableView is not visible.

If the tableView is offscreen try reloadData or manually retrieving the cell using cellForRowAtIndexPath to edit it's contents.

The documentation suggests the later if the user doesn't need to have any indication that the contents have changed.

like image 102
Olivier Wilkinson Avatar answered Oct 31 '25 16:10

Olivier Wilkinson