Question

I am trying my best to profile my app using Instruments and find out where my code is expensive. The [self checkVisibleCells] method is called in scrollViewDidScroll:(UIScrollView *)scrollView. I get about two/three cells on screen, depending on their height. I then determine if the cell is completely visible and mark it as read. Using the answer in this thread: Best way to check if UITableViewCell is completely visible

So far I can only see that self.tableView.visibleCells is taking a long time. Is it really THAT expensive to fetch visibleCells? Are there any better methods on how to do this?

Full size screenshot here: https://www.dropbox.com/s/wt8e2uat9t81qt3/Screenshot%202014-05-06%2009.26.25.png

Time profiller

Was it helpful?

Solution

The best advice than I can give you in this situation is to be sensible about when exactly you call - (NSArray *)visibleCells on the tableView.

scrollViewDidScroll gets called a hell of a lot. If you are then calling .visibleCells on the tableView every time, it is no wonder it is impacting your performance as much as it is.

My advice is that, considering you obviously know the height of your table view cells, (either declared in code or in IB), I would use this to your advantage. Note that this will not really work if your cells vary in height.

Up front, in your checkVisibleCells method, I would add this:

- (void)checkVisibleCells
{
    CGFloat newContentOffsetY = self.tableView.contentOffset.y;
    BOOL tableViewHasScrollFarEnough = newContentOffsetY > lastCheckedContentOffsetY + MysteriousViewControllerTableViewCellHeight;
    tableViewHasScrollFarEnough = tableViewHasScrollFarEnough || self.tableView.contentOffset.y < self.lastCheckedContentOffsetY - MysteriousViewControllerTableViewCellHeight;

    if (tableViewHasScrollFarEnough)
    {
        return;
    }

    self.lastCheckedContentOffsetY = self.tableView.contentOffset.y;

    // ... the rest of the method
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top