简体   繁体   中英

TableView addGesture to UIImageView to know which cell was tapped

I create tableView with sections, i use custom cell and define there checkbox(UIImageView) with image in this way:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"cellIdentifier";
    StandardCellWithImage *cell = (StandardCellWithImage *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil) {
        cell = [[StandardCellWithImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    }

    cell.checkbox.tag = indexPath.row;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSelectedImageAtIndexPath:)];
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.delegate = self;
    [cell.checkbox addGestureRecognizer:tapGesture];
    cell.checkbox.userInteractionEnabled = YES;

    return cell;
}

And in didSelectedImageAtIndexPath method i use:

- (void) didSelectedImageAtIndexPath:(id) sender {
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:gesture.view.tag inSection:0];
}

But i've got here only row without any knowledge of on which section user tap this row. Is there any possibility to recognize it?

What about if you encode item/section within view.tag like this:

view.tag = indexPath.section * kMAX_SECTION_SIZE + indexPath.item;

then you could do:

- (void) didSelectedImageAtIndexPath:(id) sender {
  UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;


  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:gesture.view.tag % kMAX_SECTION_SIZE
                                        inSection:int(gesture.view.tag / kMAX_SECTION_SIZE)];
}

Instead of adding gesture on checkbox(button in cell) in cellForRowAtIndexPath, implement button action in cell(StandardCellWithImage) itself and call delegate from there.

  1. Set action to button in cell & implement it there itself.
  2. Declare a protocol in cell & declare method in it that you want as didSelectedImageAtIndexPath:
  3. Implement this protocol in your view controller
  4. Set delegate of cells in cellForRowAtIndexPath to selt(view controller)
  5. When you tap on checkbox method in cell will get called that you set as action to checkbox button.
  6. Call delegate method didSelectedImageAtIndexPath: from there. And of course, you can return indexPath object from there using [(UITableView *)self.superview indexPathForCell:self]. // Here self = custon cell object

Note:

You can store a weak reference to the tableView in the cell, which you'd set in -tableView:cellForRowAtIndexPath: of your table's dataSource. This is better because relying on self.superview to always be exactly the tableView is fragile. Who knows how Apple might re-organize the view hierarchy of UITableView in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM