菜鸟教程小白 发表于 2022-12-12 22:12:13

ios - UITableView 滚动性能


                                            <p><p>我正在尽我最大的努力使用 Instruments 来分析我的应用,并找出我的代码成本高昂的地方。 <code></code> 方法在 <code>scrollViewDidScroll:(UIScrollView *)scrollView</code> 中调用。我在屏幕上看到大约两/三个单元格,具体取决于它们的高度。
然后我确定单元格是否完全可见并将其标记为已读。使用此线程中的答案:<a href="https://stackoverflow.com/questions/9831485/best-way-to-check-if-uitableviewcell-is-completely-visible" rel="noreferrer noopener nofollow">Best way to check if UITableViewCell is completely visible</a> </p>

<p>到目前为止,我只能看到 <code>self.tableView.visibleCells</code> 需要很长时间。获取 <code>visibleCells</code> 真的那么贵吗?有没有更好的方法来做到这一点?</p>

<p>此处为全尺寸截图:<a href="https://www.dropbox.com/s/wt8e2uat9t81qt3/Screenshot%202014-05-06%2009.26.25.png" rel="noreferrer noopener nofollow">https://www.dropbox.com/s/wt8e2uat9t81qt3/Screenshot%202014-05-06%2009.26.25.png</a> </p>

<p> <img src="/image/IhNJO.png" alt="Time profiller"/> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在这种情况下,我能给你的最好建议是明智地了解你何时在 <code>tableView</code> 上调用 <code>- (NSArray *)visibleCells</code>。</p >

<p><code>scrollViewDidScroll</code> 经常被称为 hell 。如果你每次都在 <code>tableView</code> 上调用 <code>.visibleCells</code>,难怪它会对你的性能产生如此大的影响。</p>

<p>我的建议是,考虑到您显然知道表格 View 单元格的高度(在代码中或在 IB 中声明),我会利用这一点为您带来优势。请注意,如果您的单元格高度不同,这将不会真正起作用。</p>

<p>首先,在您的 checkVisibleCells 方法中,我会添加以下内容:</p>

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

    if (tableViewHasScrollFarEnough)
    {
      return;
    }

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

    // ... the rest of the method
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableView 滚动性能,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23488809/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23488809/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableView 滚动性能