Detect Tap on UIImageView within UITableViewCell

前端 未结 6 1458
无人及你
无人及你 2021-02-20 11:40

I have a custom complex UITableViewCell where there are many views. I have an UIImageView within it which is visible for a specific condition. When it\'s visible ,

6条回答
  •  鱼传尺愫
    2021-02-20 12:42

    Maybe not the ideal solution, but add tags to each of the UIImageViews. Then have an NSArray with the uid's corresponding to the tag values

    So somewhere in your code make the array

    NSArray *testArray = [NSArray arrayWithObjects:@"uid1", @"uid2", @"uid3", @"uid4", @"uid5", @"uid6", nil];
    

    Then when you're setting up the tableview cells set the tag to the row #

    //Set the tag of the imageview to be equal to the row number 
    cell.imageView.tag = indexPath.row;
    
    //Sets up taprecognizer for each imageview
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(handleTap:)];
    [cell.imageView addGestureRecognizer:tap];
    
    //Enable the image to be clicked 
    cell.imageView.userInteractionEnabled = YES;
    

    Then in the method that gets called you can get the tag like this

    - (void)handleTap:(UITapGestureRecognizer *)recognizer  
    {    
         NSString *uid = testArray[recognizer.view.tag];    
    }
    

提交回复
热议问题