Search code examples
iosobjective-cuitableviewquickdialog

UITableViewCell using QuickDialog : handle touch event on all the cell's surface


I use custom cell on UITableView using QuickDialog

In this custom cell I have several UILabel, UIImageView and a button which has the cell size and display on the top of the others subviews.

I want this button handle touch envent and call a selector. But even though the button is at the top position, the touch event does not trigger when I touch a subview.

- (UITableViewCell *)getCellForTableView:(QuickDialogTableView *)tableView controller:(QuickDialogController *)controller
{
    UITableViewCell *cell = [super getCellForTableView:tableView controller:controller];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = cell.frame;

    [btn setEnabled:YES];
    [btn setExclusiveTouch:YES];
    [btn setBackgroundColor:[UIColor blueColor]]; // To check if the button is at the top position
    [btn setStringTag:labelIdText];
    [btn addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];

    [cell addSubview:label1];
    [cell addSubview:label2];
    [cell addSubview:image1];
    [cell addSubview:image2];
    [cell addSubview:btn];

    cell.userInteractionEnabled = YES;

    return cell;
}

The selector :

- (IBAction)handleTap:(id)sender
{

    NSLog(@"CELL TAPPED : \n");
}

Thanks a lot.

EDIT :

New version of the code :

- (UITableViewCell *)getCellForTableView:(QuickDialogTableView *)tableView controller:(QuickDialogController *)controller
{
    UITableViewCell *cell = [super getCellForTableView:tableView controller:controller];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [top setStringTag:labelIdText];
    [top addGestureRecognizer:singleFingerTap];

    [cell.contentView addSubview:label_1];
    [cell.contentView addSubview:label_2];
    [cell.contentView addSubview:image_1];
    [cell.contentView addSubview:image_2];
    [cell.contentView addSubview:top];

    cell.userInteractionEnabled = YES;
    image_1.userInteractionEnabled = YES;
    image_2.userInteractionEnabled = YES;

    return cell;
}

The selector :

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{

    NSLog(@"CELL TAPPED : \n");
}

The event is handle when I touch the text containing in the UILabel. But it's still don't work with the UIImageView despite userInteractionEnabled setted to YES for the images.

Thanks again.


Solution

  • It seams you are assigning to the button the cell frame and then add it to the cell itself. In this way the position of the button is not the one you expect, and even though you can see the button it will not respond to touch because it resides outside the cell bounds. Try to change this:

    btn.frame = cell.frame;
    

    to this:

    btn.frame = cell.bounds;
    

    Also when working with UItableViewCell remember to add subviews to its contentView and not the cell itself:

    [cell.contentView addSubview:aCustomView];