<small id='GiDDr'></small><noframes id='GiDDr'>

      <legend id='GiDDr'><style id='GiDDr'><dir id='GiDDr'><q id='GiDDr'></q></dir></style></legend>
        <bdo id='GiDDr'></bdo><ul id='GiDDr'></ul>
    1. <tfoot id='GiDDr'></tfoot>

      <i id='GiDDr'><tr id='GiDDr'><dt id='GiDDr'><q id='GiDDr'><span id='GiDDr'><b id='GiDDr'><form id='GiDDr'><ins id='GiDDr'></ins><ul id='GiDDr'></ul><sub id='GiDDr'></sub></form><legend id='GiDDr'></legend><bdo id='GiDDr'><pre id='GiDDr'><center id='GiDDr'></center></pre></bdo></b><th id='GiDDr'></th></span></q></dt></tr></i><div id='GiDDr'><tfoot id='GiDDr'></tfoot><dl id='GiDDr'><fieldset id='GiDDr'></fieldset></dl></div>
      1. 具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动

        时间:2023-09-13
          <tbody id='Anngi'></tbody>

        1. <legend id='Anngi'><style id='Anngi'><dir id='Anngi'><q id='Anngi'></q></dir></style></legend>

          <small id='Anngi'></small><noframes id='Anngi'>

        2. <tfoot id='Anngi'></tfoot>
            • <bdo id='Anngi'></bdo><ul id='Anngi'></ul>
                <i id='Anngi'><tr id='Anngi'><dt id='Anngi'><q id='Anngi'><span id='Anngi'><b id='Anngi'><form id='Anngi'><ins id='Anngi'></ins><ul id='Anngi'></ul><sub id='Anngi'></sub></form><legend id='Anngi'></legend><bdo id='Anngi'><pre id='Anngi'><center id='Anngi'></center></pre></bdo></b><th id='Anngi'></th></span></q></dt></tr></i><div id='Anngi'><tfoot id='Anngi'></tfoot><dl id='Anngi'><fieldset id='Anngi'></fieldset></dl></div>

                  本文介绍了具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我觉得这可能是一个常见问题,想知道是否有任何常见的解决方案.

                  I feel like this might be a common issue and was wondering if there was any common solution to it.

                  基本上,我的 UITableView 具有每个单元格的动态单元格高度.如果我不在 UITableView 的顶部并且我 tableView.reloadData(),向上滚动会变得跳跃.

                  Basically, my UITableView has dynamic cell heights for every cell. If I am not at the top of the UITableView and I tableView.reloadData(), scrolling up becomes jumpy.

                  我相信这是因为我重新加载了数据,当我向上滚动时,UITableView 正在重新计算每个单元格的高度,以使其可见.如何减轻这种情况,或者如何仅将某个 IndexPath 中的数据重新加载到 UITableView 的末尾?

                  I believe this is due to the fact that because I reloaded data, as I'm scrolling up, the UITableView is recalculating the height for each cell coming into visibility. How do I mitigate that, or how do I only reloadData from a certain IndexPath to the end of the UITableView?

                  此外,当我设法一直滚动到顶部时,我可以向下滚动然后向上滚动,没有跳跃的问题.这很可能是因为 UITableViewCell 的高度已经计算过了.

                  Further, when I do manage to scroll all the way to the top, I can scroll back down and then up, no problem with no jumping. This is most likely because the UITableViewCell heights were already calculated.

                  推荐答案

                  为了防止跳跃,你应该在单元格加载时保存它们的高度,并在 tableView:estimatedHeightForRowAtIndexPath 中给出准确的值:

                  To prevent jumping you should save heights of cells when they loads and give exact value in tableView:estimatedHeightForRowAtIndexPath:

                  斯威夫特:

                  var cellHeights = [IndexPath: CGFloat]()
                  
                  func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
                      cellHeights[indexPath] = cell.frame.size.height
                  }
                  
                  func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
                      return cellHeights[indexPath] ?? UITableView.automaticDimension
                  }
                  

                  目标 C:

                  // declare cellHeightsDictionary
                  NSMutableDictionary *cellHeightsDictionary = @{}.mutableCopy;
                  
                  // declare table dynamic row height and create correct constraints in cells
                  tableView.rowHeight = UITableViewAutomaticDimension;
                  
                  // save height
                  - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
                      [cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath];
                  }
                  
                  // give exact height value
                  - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
                      NSNumber *height = [cellHeightsDictionary objectForKey:indexPath];
                      if (height) return height.doubleValue;
                      return UITableViewAutomaticDimension;
                  }
                  

                  这篇关于具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height&quo 下一篇:自动布局中的弹簧:在 Xcode 5 中使用约束均匀分布视图

                  相关文章

                    <legend id='KBqh2'><style id='KBqh2'><dir id='KBqh2'><q id='KBqh2'></q></dir></style></legend>
                  1. <small id='KBqh2'></small><noframes id='KBqh2'>

                      <tfoot id='KBqh2'></tfoot>
                      • <bdo id='KBqh2'></bdo><ul id='KBqh2'></ul>
                      <i id='KBqh2'><tr id='KBqh2'><dt id='KBqh2'><q id='KBqh2'><span id='KBqh2'><b id='KBqh2'><form id='KBqh2'><ins id='KBqh2'></ins><ul id='KBqh2'></ul><sub id='KBqh2'></sub></form><legend id='KBqh2'></legend><bdo id='KBqh2'><pre id='KBqh2'><center id='KBqh2'></center></pre></bdo></b><th id='KBqh2'></th></span></q></dt></tr></i><div id='KBqh2'><tfoot id='KBqh2'></tfoot><dl id='KBqh2'><fieldset id='KBqh2'></fieldset></dl></div>