如何检测自定义 UITableviewcell 上按下了哪个 UILabel?

发布于 2024-12-10 18:18:18 字数 411 浏览 1 评论 0原文

我有两个标签,每个标签都有两个单独的标签。

我想通过检查标签来检测按下了哪一个标签。

在里面

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

我可以通过这样的代码检索其中之一:

cell = [walltable cellForRowAtIndexPath:indexPath];
topLabel= (UILabel *)[cell.contentView.subviews objectAtIndex:0];

但我不知道被按下的那个。

有没有一种方法可以找到用户按下了哪个标签?

I have two labels with two seperate tags each one.

I would like to detect which one label was pressed by checking the tag.

Inside the

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

i can retrieve one of them by code like this:

cell = [walltable cellForRowAtIndexPath:indexPath];
topLabel= (UILabel *)[cell.contentView.subviews objectAtIndex:0];

but i do not know the one that was pressed.

Is there a way to achieve to find which one label was pressed by the user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

热血少△年 2024-12-17 18:18:18

我想指出一些重要的事情:您对标签的引用:

topLabel= (UILabel *)[cell.contentView.subviews objectAtIndex:0];

不是正确的通用方法。我建议将单元格中的元素附加到 IBOutlet,并从那里获取引用。
至于您关于 UILabel 触摸事件的问题,我认为实现此目的的一个好方法是将 UITapGestureRecognizer 添加到您的标签,如下所示:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(firstLabelTapped)];
[firstLabel addGestureRecognizer:tgr];
[tgr release];

对第二个标签执行相同的操作。如果您想将信息传回 TableView 的视图控制器,请通过委托来执行此操作。祝你好运!

Something important I want to point out: your reference to the label:

topLabel= (UILabel *)[cell.contentView.subviews objectAtIndex:0];

is not the correct, generic way to do this. I would recommend attaching the elements in your cell to an IBOutlet, and get the reference from there.
As for your question about UILabel touch events, I think a good way to achieve this is to add a UITapGestureRecognizer to your label, like so:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(firstLabelTapped)];
[firstLabel addGestureRecognizer:tgr];
[tgr release];

Do the same with the second label. If you want to pass back information to the TableView's view controller, do this with delegation. Good luck!

无尽的现实 2024-12-17 18:18:18

您可以按照此问题和答案中的建议使用触摸事件和标签的标签。

处理 UILabel 中的触摸事件并挂钩它直到 IBAction

You can use touch events and label's tag as suggested in this question and answer.

Handling Touch Event in UILabel and hooking it up to an IBAction

初雪 2024-12-17 18:18:18

如上所述,一种方法是为每个标签分配一个标签,然后在回调中评估调用 UILabel 的标签。

如果您使用自定义单元格(即子类化的 UITableViewCell,而不是添加了自定义内容/布局的标准 UITableViewCell),另一种方法是将两个标签中的每一个简单地定义为属性你的子类细胞。如果标签被分配为各自的属性,您可以根据调用者评估这些属性并确定按下了哪个标签。

One method, as suggested above, is to assign a tag to each label, then evaluate the tag of the calling UILabel in your callback.

Another approach if you're using a custom cell (i.e., a subclassed UITableViewCell, versus a standard UITableViewCell to which you've added custom content/layout) is to simply define each of the two labels as properties of your subclassed cell. If the labels are assigned as respective properties, you can evaluate those properties against the caller and determine which label was pressed.

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