how can I distinguish which part of UITableViewCell has been clicked

前端 未结 5 1615
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 22:33

I have created a UITableView with a custom UITableViewCell. My cell includes one UIImageView on the left and UITextView o

相关标签:
5条回答
  • 2020-12-04 23:02

    Make the image a UIButton. When the button's action is triggered, you know the user tapped the image (cell will NOT be selected). If cell is selected, you know the user tapped somewhere else on the row (including text view or label).

    Also, set the button's tag property to, e.g. the cell's row index so you can know which row's image was tapped.

    0 讨论(0)
  • 2020-12-04 23:05

    Rather than adding the gesture recognisers to each individual cell, you can add one to the table view and determine which cell was selected from the point of the users touch, and then determine if the user touched the image or the cell.

    First make sure your controller adopts the UIGestureRecognizerDelegate protocol.

    @interface MyTableViewController() <UIGestureRecognizerDelegate>
    @end
    

    Then add the UIGestureRecognizer to the UITableView when the view loads.

        - (void)viewDidLoad
    {
        [super viewDidLoad];
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        singleTap.delegate = self;
        singleTap.numberOfTapsRequired = 1;
        singleTap.numberOfTouchesRequired = 1;
        [self.tableView addGestureRecognizer:singleTap];
    }
    

    This delegate method determines if the handleTap: method should be executed. If it can find an indexPath from the users touch, then it returns YES otherwise it returns NO.

    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    {
        UITableView *tableView = (UITableView *)gestureRecognizer.view;
        CGPoint p = [gestureRecognizer locationInView:gestureRecognizer.view];
        if ([tableView indexPathForRowAtPoint:p]) {
            return YES;
        }
        return NO;
    }
    

    Once we have determined if the user has clicked in a cell, the handleTap: method is called, which then decides if the user touched the image, or any other part of the cell.

    - (void)handleTap:(UITapGestureRecognizer *)tap
    {
        if (UIGestureRecognizerStateEnded == tap.state) {
            UITableView *tableView = (UITableView *)tap.view;
            CGPoint p = [tap locationInView:tap.view];
            NSIndexPath* indexPath = [tableView indexPathForRowAtPoint:p];
            [tableView deselectRowAtIndexPath:indexPath animated:NO];
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            CGPoint pointInCell = [tap locationInView:cell];
            if (CGRectContainsPoint(cell.imageView.frame, pointInCell)) {
                // user tapped image
            } else {
                // user tapped cell
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 23:05

    You could subclass UITableViewCell and override touchesEnded.

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
        [super touchesEnded:touches withEvent:event];
    
        UITouch *touch = [touches  anyObject];
        CGPoint location = [touch locationInView:self];
    
        UIView *hitView = [self hitTest:location withEvent:event];
    
        if (hitView == myImageView) ...;
        if (hitView == myTextView) ...;
    }
    

    You need to keep some reference to your UIImageView and UITextView (they should probably be properties of your cell).

    You can of course override touchesBegan instead of touchesEnded, depends on what functionality you want to achieve.

    0 讨论(0)
  • 2020-12-04 23:07

    a very abstract and general answer is to do the following For each UIImage and UILabel you add set their tag to be the indexPath.row

    //When creating the label and image add a recognizer to them
    label.tag = indexPath.row;
    imageView.tag = indexPath.row;
    

    Then add a UITapGestureRecognizer on each image and label, like so

        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                                     action:@selector(handleTap:)];
        [label addGestureRecognizer:recognizer];
        [imageView addGestureRecognizer:recognizer];
    }
    
    - (void) handleTap:(UITapGestureRecognizer*)recognizer
    {
        UIView *view = recognizer.view;
        int row = view.tag;
        if ([view isKindOfClass:[UILabel class]]) {
            //Row is row
            //and the label is pressed
        }
    
        if ([view isKindOfClass:[UIImageView class]]) {
            //Row is row
            //and the imageview is pressed
        }
    }
    
    0 讨论(0)
  • 2020-12-04 23:20

    you have two option is to implement :- 1--add UITapGestureRecognizer in your "uitableviewcell" and make it point to image view and pass "indexpath" as parameter to selector and make the delegate pass it to tableviewcell

    UILabel *label = =[UILabel alloc]init];
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture =
    [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)]     autorelease];
    [label addGestureRecognizer:tapGesture];
    
    
    -(void)labelTap{
    [delegate performselector@selector(labeltapped:)withobject:indexpath];
    }
    

    2- Second way is to check the sender of DidSelectRowAtIndexPath of type [imageview or label]; but i prefer the first way

    0 讨论(0)
提交回复
热议问题