tags:

views:

485

answers:

1

in iphone application.

I'm trying to get indexPath.row value to do something on the basis of row selected in programmatically created tableview.

Can someone please tell me why is the indexPath.row value is not coming correct. It's some thing like 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);

Thanks, Aaryan

A: 

The rows and sections of an NSIndexPath are arrays, and so start at 0 rather than 1. Therefore, the number of rows (or sections) in an NSIndexPath is actually 1 greater than the index of the last one.

Try

NSInteger lastSection = nSections - 1;
NSInteger lastRow = nRows - 1;    
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRow inSection:lastSection];
marramgrass