如何检测自定义 UITableviewcell 上按下了哪个 UILabel?
我有两个标签,每个标签都有两个单独的标签。
我想通过检查标签来检测按下了哪一个标签。
在里面
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想指出一些重要的事情:您对标签的引用:
不是正确的通用方法。我建议将单元格中的元素附加到 IBOutlet,并从那里获取引用。
至于您关于 UILabel 触摸事件的问题,我认为实现此目的的一个好方法是将 UITapGestureRecognizer 添加到您的标签,如下所示:
对第二个标签执行相同的操作。如果您想将信息传回 TableView 的视图控制器,请通过委托来执行此操作。祝你好运!
Something important I want to point out: your reference to the label:
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:
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!
您可以按照此问题和答案中的建议使用触摸事件和标签的标签。
处理 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
如上所述,一种方法是为每个标签分配一个标签,然后在回调中评估调用 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.