iOS 如何获取 cellForRowAtIndexPath 方法上当前选定的索引路径行?

发布于 2024-12-04 07:01:05 字数 946 浏览 4 评论 0原文

我想在表格的每个单元格中使用标签建立链接。

单击链接时,表格将获取单元格的[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清秋悲枫 2024-12-11 07:01:05

要确定当前选定的单元格,您可以使用 UITableView 的下一个方法:

- (NSIndexPath *)indexPathForSelectedRow

但我不确定在 UITapGestureRecognizer 触发后是否会选择您的单元格。

我建议您将单元格的行直接存储在 tag 属性中的 gestureRec.view 中:

gestureRec.view.tag = indexPath.row;

然后在 openUrl 中,您可以通过获取来确定所选单元格sender.view.tag 的值

To determine the current selected cell you can use next method of UITableView:

- (NSIndexPath *)indexPathForSelectedRow

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 in tag property:

gestureRec.view.tag = indexPath.row;

Then in openUrl you can determine the selected cell by getting value of sender.view.tag

錯遇了你 2024-12-11 07:01:05

目前还不是很清楚你想要做什么..你是否在 UITableViewCell 上放置了一个链接来触发一些其他操作?

UITableViewDelegate 为您提供了一些非常酷调用的方法

– tableView:willSelectRowAtIndexPath:
– tableView:didSelectRowAtIndexPath:
– tableView:willDeselectRowAtIndexPath:
– tableView:didDeselectRowAtIndexPath:

:点击一个单元格,将调用 willSelectRowAtIndexPathdidSelectRowAtIndexPath - 为您提供当前选定的 NSIndexPath 然后您可以使用它来获取行,如下所示:

indexPath.row;

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:

– tableView:willSelectRowAtIndexPath:
– tableView:didSelectRowAtIndexPath:
– tableView:willDeselectRowAtIndexPath:
– tableView:didDeselectRowAtIndexPath:

When you tap a cell, the willSelectRowAtIndexPath and didSelectRowAtIndexPath are called - supplying you the currently selected NSIndexPath which you can then use to get the row as follows:

indexPath.row;
傲鸠 2024-12-11 07:01:05

您可以使用全局变量来保留 indexpath.row 的值,

将行存储在 didSelectRow:atIndexPath: 方法中

var = indexPath.row;
[tableView reloadData];

var = indexPath.row;
[tableView reloadData];

,然后在 cellForRowAtIndexPath 中使用它

if(indexPath.row==var){

}

You can use a global variable to keep the value of indexpath.row

store the row in didSelectRow:atIndexPath: method

var = indexPath.row;
[tableView reloadData];

var = indexPath.row;
[tableView reloadData];

then use it in cellForRowAtIndexPath

if(indexPath.row==var){

}

蓝海似她心 2024-12-11 07:01:05

您可以在 UITableViewDelegate 中实现方法 – tableView:willSelectRowAtIndexPath:。在那里您可以轻松获取有关indexPath的信息。

You may implement method – tableView:willSelectRowAtIndexPath: in your UITableViewDelegate. There you can easily obtain information on indexPath.

独自唱情﹋歌 2024-12-11 07:01:05

这样做

您可以在 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];

you can do like this

in cellForRowAtIndexPath data source method

UIImageView* selectedBg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg_selected.png"]];

cell.backgroundView = [[UIView alloc] initWithFrame: CGRectZero];
cell.selectedBackgroundView = selectedBg;
[cell.backgroundView setNeedsDisplay];
[selectedBg release];
花开半夏魅人心 2024-12-11 07:01:05

可以使用以下代码获取当前选定的indexpath行:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {

 NSLog(@"selected tableview row is %ld",(long)indexPath.row);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文