3

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?

Sam
  • 26,946
  • 12
  • 75
  • 101
smartsanja
  • 4,413
  • 9
  • 58
  • 106

2 Answers2

5

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];
Sam
  • 26,946
  • 12
  • 75
  • 101
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
1

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];
Mohit
  • 3,708
  • 2
  • 27
  • 30
  • Please add some description to your answer to allow us mortals to understand the solution you provide. – Rachcha Mar 24 '14 at 07:51