将uitableview重新加载到底部单元格

jef*_*eff 5 iphone scroll objective-c uitableview ios

我正在创建一个聊天应用程序,我通过uitableview中的单元格显示每条消息.但是,每次重新加载tableview时,它都会显示第一个单元格中的内容.我想安排我的表,这样当它重新加载时,它会一直"滚动",从而显示最新的消息.

我已经对文档进行了一些研究,但我对编程非常新,所以我不知道如何设置它.我想我需要reloadRowsAtIndexPaths方法,但我不知道如何实现它.谢谢!

Rob*_*bin 6

您应该在表视图的末尾插入另一行,当您这样做时,您需要知道最后一行没有.

因此,您可以调用此函数来插入新行

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
Run Code Online (Sandbox Code Playgroud)

像这样

[tableView insertRowsAtIndexPaths:[NSIndexPath indexPathForRow:lastRow inSection:0] withRowAnimation:UITableViewRowAnimationNone];
lastRow++;
Run Code Online (Sandbox Code Playgroud)

然后通过调用此函数为表视图设置动画以滚动到最后一行.

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)

像这样

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:lastRow inSection:0] atScrollPosition:UITableViewScrollPositionButtom animated:YES];
Run Code Online (Sandbox Code Playgroud)


Vij*_*jay 5

用这个

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

希望这解决你的问题.