objective-c UITableView - 选择了哪一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/663853/
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
UITableView - what row was selected?
提问by Ilya Suzdalnitski
I have a question - how can I get a number of row selected in a table? I am assigning it manually to a variable. The problem is that if a row was deselected, my variable still keeps the old value.
我有一个问题 - 如何在表中选择多行?我正在将它手动分配给一个变量。问题是,如果取消选择一行,我的变量仍然保留旧值。
What can I do about it? Is there a method in UITableView that returns a number of a currently selected row?
我该怎么办?UITableView 中是否有一个方法可以返回当前选定行的编号?
Thank you in advance, Ilya.
提前谢谢你,伊利亚。
回答by Jarret Hardie
If you haven't implemented a delegate (see nduplessis), UITableView also offers:
如果您尚未实现委托(请参阅 nduplessis),UITableView 还提供:
- (NSIndexPath *)indexPathForSelectedRow
回答by nduplessis
The UITableViewDelegate will call
UITableViewDelegate 将调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
when a row is selected. From this you can easily determine the row by using indexPath.section and indexPath.row
选择行时。由此,您可以使用 indexPath.section 和 indexPath.row 轻松确定行

