Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView - cellForRowAtIndexPath delayed rendering

I have a UITableView which its cells may vary depending on the data to be shown on each, so I have two reusable cells, the UIPendingXOCell and the UIXOCell. At some point, every UIPendingXOCell becomes a UIXOCell.

See the code below for a bit of context:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var xoItem = self.xoForIndexPath(indexPath)
        var xoItemPFObject = xoItem.pfObject

        // If it's a pending XO
        if xoItemPFObject.objectId == nil {
            var cellIdentifier = "cell-pending-xo-list"
            var cell: UIPendingXOCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UIPendingXOCell!

            ...

            return cell
        }
        else {
            var cellIdentifier = "cell-xo-list"
            var cell: UIXOCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UIXOCell!

            ...

            return cell
        }
    }

Example: I have a list of 5 objects, which when calling .reloadData() generates 1 UIPendingXOCell and 4 UIXOCell (all fine). I also have an action running on the background which changes the objects in a way that when I call .reloadData() again, they all become UIXOCell (all fine again, cellForRowAtIndexPath is being called and falling into the else block).

My problem is that it takes about 10 seconds after cellForRowAtIndexPath is called for the cell to be rendered as a UIXOCell rather than a UIPendingXOCell.

Has anyone had this problem before? If so, how should I fix it?

Thanks!

like image 808
Marcos Duarte Avatar asked Dec 25 '22 03:12

Marcos Duarte


1 Answers

Sound like you are not calling reloadData from main thread (UI thread). Make sure you are updating UI from main thread.

dispatch_async(dispatch_get_main_queue()) { 
    tableView.reloadData()
}
like image 169
Kirsteins Avatar answered Dec 28 '22 08:12

Kirsteins