How do I know which row is currently selected in table view without using any global variable to store last clicked row using this method.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
How do I know which row is currently selected in table view without using any global variable to store last clicked row using this method.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
アウトレットプロパティにUITableViewがある場合、次のように選択した行を取得できます。
[tableView indexPathForSelectedRow]
これは、選択した行の行番号とセクション番号を含むindexPathを返します。
You don't need a global variable. if you need to track the currently selected row, you can create a member variable (iVar) in the class that is the delegate for the table view callbacks.
That method passes you the indexPath which has the row that was selected. If you want remember that for other function callbacks, then create a property or iVar in that class.
In header:
@interface MyTableViewController : UITableViewController {
NSInteger _selectedRow;
}
In implementation
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedRow = indexPath.row;
}
私はこのようにしました...。
NSIndexPath *selectedIndexPath = [table indexPathForSelectedRow];
Indexpathを使用して行を取得しました。
編集:NSUserDefaultに保存
このデリゲート メソッドでは、現在選択されている行の追加 int curRowIndex を .h ファイルで取得できます。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
curRowIndex = indexPath.row
NSLog(@"Currently Selected Row: %d",curRowIndex);
}
If your table contains more that one section then you have to track the section also.
For that you have to create an object of NSIndexPath
.
@interface MyTableViewController : UITableViewController {
NSIndexPath *obj_IndexPath;
}
and in implementation file,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
obj_IndexPath = indexPath;
NSLog(@"%d",obj_IndexPath.row);
NSLog(@"%d",obj_IndexPath.section);
}
and don't forget to release
obj_IndexPath
in dealloc
method
私はこれを使用します:
if(indexPath.row == 0){
// do something...
}else if(indexPath.row == 1){
// do something else ...
}