获取UITableView中最后一行的索引路径

时间:2014-07-31 06:27:26

标签: ios objective-c uitableview

我有一个UITableView包含1个部分,numberOfRows:来自NSMutableArray

我想要的是每当UITableView出现时,滚动视图必须滚动到最后一行。

为此,我想要最后一行的indexPath。如何获得它?

我知道这个问题已被多次回答。但这些方法都不适合我。

这是我试过的代码

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

2 个答案:

答案 0 :(得分:5)

由于您只有一个部分,因此可以使用

找到索引路径
[NSIndexPath indexPathForRow:[yourArray count]-1 inSection:0]

答案 1 :(得分:0)

scrollToRowAtIndexPath应该有用。

viewWillAppear中,试试这个:

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