简体   繁体   English

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

[英]reload uitableview to bottom cell

im creating a chat app, and I display each message thru a cell in the uitableview. 我正在创建一个聊天应用程序,我通过uitableview中的单元格显示每条消息。 However, each time i reload the tableview, it displays the content from the first cell down. 但是,每次重新加载tableview时,它都会显示第一个单元格中的内容。 I want to arrange my table so that when it reloads, it is "scrolled" all the way down, thus displaying the most recent message. 我想安排我的表,这样当它重新加载时,它会一直“滚动”,从而显示最新的消息。

I've done some research on the docs, but im very very new to programming so im not sure how to set it up. 我已经对文档进行了一些研究,但我对编程非常新,所以我不知道如何设置它。 I think i need the reloadRowsAtIndexPaths method, but im not sure how to implement it. 我想我需要reloadRowsAtIndexPaths方法,但我不知道如何实现它。 Thanks! 谢谢!

You should insert another row at the end of the table view, when you do that you need to know the last row no. 您应该在表视图的末尾插入另一行,当您这样做时,您需要知道最后一行没有。

So you can call this function to insert the new row 因此,您可以调用此函数来插入新行

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

like this 像这样

[tableView insertRowsAtIndexPaths:[NSIndexPath indexPathForRow:lastRow inSection:0] withRowAnimation:UITableViewRowAnimationNone];
lastRow++;

and then animate the table view to scroll to last row by calling this function. 然后通过调用此函数为表视图设置动画以滚动到最后一行。

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

like this 像这样

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:lastRow inSection:0] atScrollPosition:UITableViewScrollPositionButtom animated:YES];

Use This 用这个

int lastRowNumber = [tableView numberOfRowsInSection:0]-1;
NSIndexPath* ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
[tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:YES];

Hope this Solve Ur problem. 希望这解决你的问题。

[self.tableView reloadData]; [self.tableView reloadData]; [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.messages count]-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.messages count] -1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

Scrolling the table view can easily be achieved using the –scrollToRowAtIndexPath:atScrollPosition:animated: method: 使用–scrollToRowAtIndexPath:atScrollPosition:animated:方法可以轻松实现滚动表视图:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1000 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1000 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

Of course, you need to adjust the values for the indexPath. 当然,您需要调整indexPath的值。

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

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