iOS 如何获取 cellForRowAtIndexPath 方法上当前选定的索引路径行?
我想在表格的每个单元格中使用标签建立链接。
单击链接时,表格将获取单元格的[indexpath row],我们将使用该索引与包含字符串数据的数组索引进行匹配。该字符串将被发送到下一个推送页面。
我正在使用 UITapGestureRecognizer 来点击标签并将参数放入选择器方法。
如何获取当前索引路径行所选单元格上的标签? 这是我的示例代码:
-(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];
}
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.
I'm using UITapGestureRecognizer to tap the label and put parameter to selector method.
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要确定当前选定的单元格,您可以使用
UITableView
的下一个方法:但我不确定在
UITapGestureRecognizer
触发后是否会选择您的单元格。我建议您将单元格的行直接存储在
tag
属性中的gestureRec.view
中:然后在
openUrl
中,您可以通过获取来确定所选单元格sender.view.tag
的值To determine the current selected cell you can use next method of
UITableView
:But I'm not sure that your cell will be selected after
UITapGestureRecognizer
fired.I advice you to store row of the cell directly in
gestureRec.view
intag
property:Then in
openUrl
you can determine the selected cell by getting value ofsender.view.tag
目前还不是很清楚你想要做什么..你是否在 UITableViewCell 上放置了一个链接来触发一些其他操作?
UITableViewDelegate
为您提供了一些非常酷调用的方法:点击一个单元格,将调用
willSelectRowAtIndexPath
和didSelectRowAtIndexPath
- 为您提供当前选定的NSIndexPath
然后您可以使用它来获取行,如下所示:It is not very clear what you want to do.. do you have a link layed out on the
UITableViewCell
which triggers some other actions?The
UITableViewDelegate
gives you some really cool methods called:When you tap a cell, the
willSelectRowAtIndexPath
anddidSelectRowAtIndexPath
are called - supplying you the currently selectedNSIndexPath
which you can then use to get the row as follows:您可以使用全局变量来保留
indexpath.row
的值,将行存储在
didSelectRow:atIndexPath:
方法中,然后在
cellForRowAtIndexPath
中使用它}
You can use a global variable to keep the value of
indexpath.row
store the row in
didSelectRow:atIndexPath:
methodthen use it in
cellForRowAtIndexPath
}
您可以在
UITableViewDelegate
中实现方法– tableView:willSelectRowAtIndexPath:
。在那里您可以轻松获取有关indexPath的信息。You may implement method
– tableView:willSelectRowAtIndexPath:
in yourUITableViewDelegate
. There you can easily obtain information on indexPath.这样做
您可以在 cellForRowAtIndexPath 数据源方法中
you can do like this
in cellForRowAtIndexPath data source method
可以使用以下代码获取当前选定的indexpath行:
The following code can be used to obtain the current selected indexpath row: