如何获得IndexPath

use*_*883 2 iphone

在iPhone应用程序中.

我正在尝试获取indexPath.row值,以在以编程方式创建的tableview中选择的行为基础执行某些操作.

有人可以告诉我为什么indexPath.row值不正确.有点像21487 ...... 3?

NSUInteger nSections = [myTableView numberOfSections];
NSUInteger nRows = [myTableView numberOfRowsInSection:nSections];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: nRows inSection:nSections];
NSLog(@"No of sections in my table view %d",nSections); 
NSLog(@"No of rows in my table view %d",nRows);
NSLog(@"Value of selected indexPath Row %d", indexPath.row);
Run Code Online (Sandbox Code Playgroud)

谢谢,Aaryan

mar*_*ass 7

a的行和部分NSIndexPath是数组,因此从0开始而不是1.因此,a中的行(或部分)的NSIndexPath数量实际上比最后一个的索引大1.

尝试

NSInteger lastSection = nSections - 1;
NSInteger lastRow = nRows - 1;    
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRow inSection:lastSection];
Run Code Online (Sandbox Code Playgroud)