菜鸟教程小白 发表于 2022-12-13 02:14:16

ios - 在 iOS 8 中停止 UITableView 的自动滚动


                                            <p><p>当我使用 <code>;</code></p>

<p><strong><em>详细问题:</em></strong></p>

<p>现在我将详细说明我是如何解决这个问题的,尤其是在 iOS 8 中。选取任何具有大约 50 个条目的 tableview,向下滚动以使第 10 个条目位于 tableview 的顶部。然后选择 tableview 上的任何项目。使用下面的方法将 ViewController 推送到 tableview 中的行选择。</p>

<pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ;

    // Open the New View
    NewViewController *newVC = [ init];
    ;
}
</code></pre>

<p>现在你会发现从 <code>NewViewController</code> 回到之前的 ViewController 时,tableview 会自动滚动一段距离。 tableview 不会停留在滚动的地方,它总是会自动改变它的位置。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我遇到了同样的问题。我正在使用新的 iOS 8 功能动态单元格高度。我正在设置:</p>

<pre><code>self.tableView.estimatedRowHeight = 50.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
</code></pre>

<p>问题是某些单元格远高于 50。解决方案是提供委托(delegate)方法 <code>estimatedHeightForRowAtIndexPath</code> 并返回更接近实际单元格高度的值。例如:</p>

<pre><code>-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 1) {
      return 300.0;
    }

    return 50;
}
</code></pre>

<p>另一个解决方案是使用 <code>cellForRowAtIndexPath</code> 内的 <code>systemLayoutSizeFittingSize</code> 计算单元格高度并缓存该值。 <code>estimatedHeightForRowAtIndexPath</code> 内部提供缓存值,但如果单元格高度尚未缓存,则返回默认值。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 iOS 8 中停止 UITableView 的自动滚动,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/26037796/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/26037796/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 iOS 8 中停止 UITableView 的自动滚动