如何将UITableView滚动到tableFooterView

Kev*_*vin 23 iphone uitableview

我有一个UITableView,其内容是动态变化的,就像一个FIFO堆栈.将细胞添加到底部并从顶部移除.

这很好用,我可以滚动到indexPath,这样最新的消息总是向下滚动到底部(就像聊天应用程序一样).

现在..我想在该表部分添加一个页脚.而不是使用

SrollToRowAtIndexPath

我希望能够滚动到tableFooterView.

任何想法如何我能做到这一点将不胜感激.

Jes*_*edc 27

滚动UITableView到底部的footerView 的最佳方法是简单地设置内容偏移量.您可以使用contentSize和当前计算底部bounds

这是我的方式.

CGPoint newContentOffset = CGPointMake(0, [self.instanceOfATableView contentSize].height -  self.instanceOfATableView.bounds.size.height);

[self.instanceOfATableView setContentOffset:newContentOffset animated:YES]; 
Run Code Online (Sandbox Code Playgroud)


Ned*_*Ned 27

我使用它滚动到tableView的页脚视图:

[self.tableView scrollRectToVisible:[self.tableView convertRect:self.tableView.tableFooterView.bounds fromView:self.tableView.tableFooterView] animated:YES];
Run Code Online (Sandbox Code Playgroud)

  • 简单易用! (2认同)

Bej*_*jil 15

感谢iphone_developer,这就是我所做的:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[tableView numberOfRowsInSection:0]-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
Run Code Online (Sandbox Code Playgroud)

然后,当我添加行时,我正在调用它,并且我的tableView的页脚视图保持可见

  • 不,[tableView numberOfRowsInSection:0] -1是最后一行,因此在此行上滚动到positionTop实际上会将tableView滚动到底部 (2认同)
  • 这很有趣,因为我正在这样做,但是滚动到底部,隐藏了页脚。用滚动位置顶部调用它是有效的。谢谢 (2认同)

jun*_*cat 14

这么多糟糕的答案.:-)

最好的方法:

[self.tableView scrollRectToVisible:
     self.tableView.tableFooterView.frame animated:YES
];
Run Code Online (Sandbox Code Playgroud)

  • @Kumar KL,这不适用于自定义页脚和多个页脚。 (2认同)

mah*_*udz -4

我没试过,但是当你滚动到最后一行+1时会发生什么?

另一个想法是始终在末尾添加一个虚拟条目并使其具有不同的外观,以便将其视为页脚。然后您可以随时滚动到该位置。