2009-08-14 4 views
2

iphoneアプリケーションでIndexPathを取得する方法

私はプログラムで作成されたテーブルビューで選択された行に基づいて何かを行うindexPath.row値を取得しようとしています。

誰かが、なぜ、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); 

おかげで、

Aaryan

答えて

7

NSIndexPathの行とセクションが配列され、そして従って、NSIndexPathの行(またはセクション)の数は、実際に1が0ではなく1から始まり最後のインデックスよりも大きい。

NSInteger lastSection = nSections - 1; 
NSInteger lastRow = nRows - 1;  
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRow inSection:lastSection]; 
をお試しください
関連する問題