简体   繁体   English

将单元格添加到UITableView的顶部

[英]Add cells to top of UITableView

I want to add rows to a table view without the visible cells being moved, similarly to Twitter for iPhone, Tweetbot and several others. 我想在不移动可见单元格的情况下向表格视图添加行,这与iPhone,Tweetbot和其他几个推特类似。 Every method I have tried thus far accomplishes that eventually, but does funky animations in between. 到目前为止,我尝试过的每种方法最终都可以实现这一目标,但是在两者之间却要进行时髦的动画处理。 Here is the current solution, which moves cells around but eventually ends up staying in the same place. 这是当前的解决方案,该解决方案可移动单元格,但最终最终停留在同一位置。

- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller
{
    UITableViewCell *referenceCell;
    if ([self.tableView.visibleCells count] > 0) {
        referenceCell = [self.tableView.visibleCells lastObject];
    }
    CGFloat offset = referenceCell.frame.origin.y - self.tableView.contentOffset.y;

    [self.tableView endUpdates];

    [self.tableView setContentOffset:CGPointMake(0.0, referenceCell.frame.origin.y - offset) animated:NO];
}

Try this: 尝试这个:

  1. Record the offset to the first visible cell just as before 像以前一样记录到第一个可见单元的偏移量
  2. Make sure that UITableView dataSource already contains data for new items. 确保UITableView数据源已经包含新项目的数据。
  3. Call -insertRowsAtIndexPaths:withRowAnimation: on the UITableView with UITableViewRowAnimationNone . 使用UITableViewRowAnimationNoneUITableView上调用-insertRowsAtIndexPaths:withRowAnimation: You can specify all index paths that you want to insert in one go. 您可以一次性指定要插入的所有索引路径。 The number of inserted rows must match the number of new items. 插入的行数必须与新项目的数量匹配。
  4. Call -setContentOffset: just as before 调用-setContentOffset:和以前一样

Avoid calling -beginUpdates and -endUpdates on the table view. 避免在表视图上调用-beginUpdates-endUpdates -endUpdates causes animations to be done for the original cells. -endUpdates导致对原始单元进行动画处理。

It seems that insertRowsAtIndexPaths alone may animate cells. 似乎单独的insertRowsAtIndexPaths可以使单元动画。 You could try a workaround like this: 您可以尝试这样的解决方法:

for (UITableViewCell *cell in [tableView visibleCells])
{
  [cell.layer removeAllAnimations];
}

Could you just reload the data source, and immediately call 您能否重新加载数据源,然后立即调用

[tableview selectRowAtIndexPath: animated:NO scrollPosition:<#(UITableViewScrollPosition)#>];

You would probably need to get the current position which you could do with 您可能需要获取可以处理的当前职位

[[tableview indexPathsForVisibleRows] objectAtIndex:0];

I haven't tested this out though, so I'm not 100% on it. 我还没有测试过,所以我不是100%。

Just posted an answer with code snippets here 刚刚在此处发布了带有代码段的答案

Keep uitableview static when inserting rows at the top 在顶部插入行时使uitableview保持静态

Basically: 基本上:

  • Save the scroll offset where you would have called beginUpdate:. 将滚动偏移量保存在您将要调用beginUpdate:的位置。
  • In place of calls to InsertCell you add the cell's height to the saved offset. 代替对InsertCell的调用,将单元格的高度添加到已保存的偏移量中。
  • Where you would have called endUpdate:, call reloadData, then scroll by the saved offset with NO animation. 在您将要调用endUpdate:的地方,调用reloadData,然后以NO动画滚动保存的偏移量。

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

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