Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reload uitableview to bottom cell

im creating a chat app, and I display each message thru a cell in the uitableview. However, each time i reload the tableview, it displays the content from the first cell down. 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. Thanks!

like image 232
jeff Avatar asked Mar 11 '11 09:03

jeff


2 Answers

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];
like image 128
Robin Avatar answered Sep 18 '22 11:09

Robin


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.

like image 34
Vijay Avatar answered Sep 16 '22 11:09

Vijay