UITableView在滚动时插入顶部 [英] UITableView inserting section at top while scrolling

查看:166
本文介绍了UITableView在滚动时插入顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在滚动顶部时插入UITableView的新部分(部分包含3个单元格)。

I am inserting new section (section contains 3 cells) top of UITableView while SCROLLING TOP.

      [_mainTable beginUpdates];
      [_mainTable insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
      [_mainTable endUpdates];

部分插入正确。但它需要我在Table的顶部,即单元格0或行0.我希望此事务顺利进行。我可以插入

Section gets inserting properly. But it takes me top of Table i.e. cell 0 or row 0. I want this transaction to be smooth. I can insert

[_mainTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1] atScrollPosition:UITableViewScrollPositionBottom animated:NO];

但它显示快速混蛋,因为它会将你带到桌面顶部然后突然滚动到你的最后位置。

after endUpdates but it shows quick jerk because it takes you top of Table then suddenly scroll it to your last position.

如何让它顺利进行。

谢谢

推荐答案

我还没有对此进行详尽的测试,但这看起来很有希望:

I haven't done exhaustive testing on this, but this seems promising:


  1. 识别其中一个可见单元格的 NSIndexPath

  2. 获取 rectForRowAtIndexPath

  3. 获取当前的 contentOffset 表格本身。

  4. 添加部分,但调用 reloadData 而不是 insertSections (可防止震动滚动)。

  5. 获取第2步中获得的更新 rectForRowAtIndexPath

  6. 按步骤5和步骤2的结果差异更新 contentOffset

  1. Identify NSIndexPath of one of the visible cells.
  2. Get its rectForRowAtIndexPath.
  3. Get the current contentOffset of the table, itself.
  4. Add the section, but call reloadData instead of insertSections (which prevents jarring scrolling).
  5. Get the updated rectForRowAtIndexPath that you got in step 2.
  6. Update contentOffset by the difference of the result from step 5 and that of step 2.

因此:

[self.sections insertObject:newSection atIndex:0];  // this is my model backing my table

NSIndexPath *oldIndexPath = self.tableView.indexPathsForVisibleRows[0];    // 1
CGRect before = [self.tableView rectForRowAtIndexPath:oldIndexPath];       // 2
CGPoint contentOffset = [self.tableView contentOffset];                    // 3
[self.tableView reloadData];                                               // 4
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:oldIndexPath.row inSection:oldIndexPath.section + 1];
CGRect after = [self.tableView rectForRowAtIndexPath:newIndexPath];        // 5
contentOffset.y += (after.origin.y - before.origin.y);
self.tableView.contentOffset = contentOffset;                              // 6

这篇关于UITableView在滚动时插入顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆