Search code examples
iosios4uitableviewuilabel

How to detect which UILabel was pressed on custom UITableviewcell?


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?


Solution

  • 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!