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?

stefanosn
  • 3,264
  • 10
  • 53
  • 79

3 Answers3

0

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

Community
  • 1
  • 1
user523234
  • 14,323
  • 10
  • 62
  • 102
0

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.

isaac
  • 4,867
  • 1
  • 21
  • 31
0

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!

Stavash
  • 14,244
  • 5
  • 52
  • 80