简体   繁体   English

UITableView滚动性能

[英]UITableView scrolling performance

I am trying my best to profile my app using Instruments and find out where my code is expensive. 我正在尽力使用Instruments分析我的应用程序,并找出我的代码在哪里昂贵。 The [self checkVisibleCells] method is called in scrollViewDidScroll:(UIScrollView *)scrollView . scrollViewDidScroll:(UIScrollView *)scrollView调用[self checkVisibleCells]方法。 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 在此线程中使用答案: 检查UITableViewCell是否完全可见的最佳方法

So far I can only see that self.tableView.visibleCells is taking a long time. 到目前为止,我只能看到self.tableView.visibleCells需要很长时间。 Is it really THAT expensive to fetch visibleCells ? 获取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 完整尺寸截图在这里: https : //www.dropbox.com/s/wt8e2uat9t81qt3/Screenshot%202014-05-06%2009.26.25.png

时间补充

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 . 在这种情况下,我能给您的最佳建议是明智地确定何时在tableView上准确调用- (NSArray *)visibleCells

scrollViewDidScroll gets called a hell of a lot. scrollViewDidScroll被称为地狱。 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. 如果您每次都在tableView上调用.visibleCells ,那也就不足为奇了。

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. 我的建议是,考虑到您显然知道表格视图单元的高度(无论是在代码中还是在IB中声明),我将以此为您优势。 Note that this will not really work if your cells vary in height. 请注意,如果您的单元格高度不同,这将不会真正起作用。

Up front, in your checkVisibleCells method, I would add this: 首先,在您的checkVisibleCells方法中,添加以下内容:

- (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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM