Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autoscroll tableview when a new item added to it?

How to autoscroll tableview when a new item is added to it?

I tred to use

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

But I couldn't figure it out.

Can you please provide sample code?

like image 608
smartsanja Avatar asked Jan 01 '26 11:01

smartsanja


2 Answers

For better guidance, you should see the docs for [UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]

Below is the instant solution....

int row = [dataController count] - 1;
int section = 0;
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
[[self tableView] scrollToRowAtIndexPath:scrollIndexPath
                        atScrollPosition:UITableViewScrollPositionBottom 
                                animated:YES];
like image 107
alloc_iNit Avatar answered Jan 03 '26 03:01

alloc_iNit


Add this line to your code from where you wants to scroll tableview.

[myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[_databaseArray count]-1 inSection:0]
                   atScrollPosition:UITableViewScrollPositionBottom
                           animated:YES];
like image 29
Mohit Avatar answered Jan 03 '26 04:01

Mohit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!