简体   繁体   中英

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;
 }

由于您只有一个部分,因此yu可以找到带有

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

scrollToRowAtIndexPath should work.

In viewWillAppear , try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM