xcode 如何获得选定的行 IOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11877080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get selected row IOS
提问by Feroz
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
回答by Danny Lagrouw
If you have the UITableView in an outlet property, you can get the selected row like this:
如果在 outlet 属性中有 UITableView,则可以像这样获取所选行:
[tableView indexPathForSelectedRow]
which will return an indexPath containing row and section numbers of the selected row.
这将返回一个包含所选行的行号和节号的 indexPath。
回答by bryanmac
You don't need a global variable. ifyou 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.
您不需要全局变量。 如果您需要跟踪当前选定的行,您可以在作为表视图回调委托的类中创建一个成员变量 (iVar)。
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.
该方法向您传递具有所选行的 indexPath。如果您想为其他函数回调记住这一点,请在该类中创建一个属性或 iVar。
In header:
在标题中:
@interface MyTableViewController : UITableViewController {
NSInteger _selectedRow;
}
In implementation
实施中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedRow = indexPath.row;
}
回答by Paresh Navadiya
Edit: Store in NSUserDefault
编辑:存储在 NSUserDefault 中
In this delegate method u can get Currently Selected Row Add int curRowIndex in .h file
在此委托方法中,您可以获得当前选定的行 Add int curRowIndex in .h 文件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
curRowIndex = indexPath.row
NSLog(@"Currently Selected Row: %d",curRowIndex);
}
回答by Feroz
I have done this way ....
我已经这样做了......
NSIndexPath *selectedIndexPath = [table indexPathForSelectedRow];
Using Indexpath I got row.
使用 Indexpath 我得到了行。
回答by Ankur
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
.
如果您的表格包含不止一个部分,那么您还必须跟踪该部分。为此,您必须创建一个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
并且不要忘记release
obj_IndexPath
在dealloc
方法中
回答by LAOMUSIC ARTS
I use this:
我用这个:
if(indexPath.row == 0){
// do something...
}else if(indexPath.row == 1){
// do something else ...
}