ios iOS如何获取cellForRowAtIndexPath方法上当前选定的索引路径行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7388254/
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
iOS how to get, current selected indexpath row on cellForRowAtIndexPath method?
提问by dagger24
I want to make a link using label in every cell of table.
我想在表格的每个单元格中使用标签创建一个链接。
When the link is clicked, the table will get the [indexpath row]of the cell and we will use the index to match with the array index containing string data. The string will be sent to the next push page.
单击链接时,表格将获得单元格的[indexpath row],我们将使用该索引与包含字符串数据的数组索引进行匹配。该字符串将被发送到下一个推送页面。
I'm using UITapGestureRecognizerto tap the label and put parameter to selector method.
我正在使用UITapGestureRecognizer来点击标签并将参数放入选择器方法。
How to get the current indexpath row the label on the selected cell? This is my sample code :
如何获取当前索引路径行所选单元格上的标签?这是我的示例代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openUrl:) ];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[cell.listPrice addGestureRecognizer:gestureRec];
[gestureRec release];
...
}
- (void)openUrl:(id)sender
{
NSLog(@"DOwnload URL send >> %@",urlDownloadSend);
DownloadNowController *download =[[DownloadNowController alloc]initWithNibName:@"DownloadNowController" bundle:nil];
[self.navigationController pushViewController:download animated:YES];
[download release];
}
回答by Nekto
To determine the current selected cell you can use next method of UITableView:
要确定当前选定的单元格,您可以使用以下方法UITableView:
- (NSIndexPath *)indexPathForSelectedRow
But I'm not sure that your cell will be selected after UITapGestureRecognizerfired.
但我不确定您的单元格会在被UITapGestureRecognizer解雇后被选中。
I advice you to store row of the cell directly in gestureRec.viewin tagproperty:
我建议您将单元格的行直接存储gestureRec.view在tag属性中:
gestureRec.view.tag = indexPath.row;
Then in openUrlyou can determine the selected cell by getting value of sender.view.tag
然后在openUrl您可以通过获取值来确定选定的单元格sender.view.tag
回答by Faizan S.
It is not very clear what you want to do.. do you have a link layed out on the UITableViewCellwhich triggers some other actions?
不是很清楚你想做什么..你有一个链接,UITableViewCell它会触发其他一些动作吗?
The UITableViewDelegategives you some really coolmethods called:
该UITableViewDelegate给你一些很酷的方法叫做:
– tableView:willSelectRowAtIndexPath:
– tableView:didSelectRowAtIndexPath:
– tableView:willDeselectRowAtIndexPath:
– tableView:didDeselectRowAtIndexPath:
When you tap a cell, the willSelectRowAtIndexPathand didSelectRowAtIndexPathare called - supplying you the currently selected NSIndexPathwhich you can then use to get the row as follows:
当您点击一个单元格时,willSelectRowAtIndexPath和didSelectRowAtIndexPath被调用 - 为您提供当前选择的内容NSIndexPath,然后您可以使用它来获取行,如下所示:
indexPath.row;
回答by Virat Naithani
You can use a global variable to keep the value of indexpath.row
您可以使用全局变量来保持值 indexpath.row
store the row in didSelectRow:atIndexPath:method
将行存储在didSelectRow:atIndexPath:方法中
var = indexPath.row;
[tableView reloadData];
var = indexPath.row;
[tableView reloadData];
then use it in cellForRowAtIndexPath
然后在 cellForRowAtIndexPath
if(indexPath.row==var){
}
}
回答by Manlio
You may implement method – tableView:willSelectRowAtIndexPath:in your UITableViewDelegate. There you can easily obtain information on indexPath.
您可以– tableView:willSelectRowAtIndexPath:在您的UITableViewDelegate. 在那里您可以轻松获取有关 indexPath 的信息。
回答by Praveen-K
you can do like this
你可以这样做
in cellForRowAtIndexPath data source method
在 cellForRowAtIndexPath 数据源方法中
UIImageView* selectedBg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg_selected.png"]];
cell.backgroundView = [[UIView alloc] initWithFrame: CGRectZero];
cell.selectedBackgroundView = selectedBg;
[cell.backgroundView setNeedsDisplay];
[selectedBg release];
回答by Frank
The following code can be used to obtain the current selected indexpath row:
以下代码可用于获取当前选定的索引路径行:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
NSLog(@"selected tableview row is %ld",(long)indexPath.row);
}

