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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With