Sushil Sharma
Sushil Sharma

Reputation: 2361

Get the index path of last row in UITableView

I have a UITableView with 1 section and numberOfRows: from NSMutableArray.

What I want here is that whenever UITableView appears the scroll view must scroll to last row.

For this I want indexPath of last row. How to get it?

I know this question is answered many times. But none of those approaches worked for me.

Here is the code I tried

-(NSIndexPath*)lastIndexPath{
     NSInteger sectionsAmount = [myTableView numberOfSections];
     NSInteger rowsAmount = [myTableView numberOfRowsInSection:sectionsAmount-1];
     NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:(rowsAmount - 1) inSection:(sectionsAmount - 1)];
     return lastIndexPath;
 }

Upvotes: 0

Views: 5557

Answers (2)

Rajesh
Rajesh

Reputation: 10424

Since you have only one section yu can find the indexpath with

[NSIndexPath indexPathForRow:[yourArray count]-1 inSection:0]

Upvotes: 5

Kanan Vora
Kanan Vora

Reputation: 2122

scrollToRowAtIndexPath should work.

In viewWillAppear, try this:

[theTableView reloadData];    
NSIndexPath* ip = [NSIndexPath indexPathForRow:rowNumberHere inSection:sectionNumberHere];
[theTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];

Upvotes: 0

Related Questions