how can I distinguish which part of UITableViewCell has been clicked

前端 未结 5 1633
伪装坚强ぢ
伪装坚强ぢ 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: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
        }
    }
    

提交回复
热议问题