UITapGestureRecognizer partially works with UIImageView inside UITableViewCell

I have a tabular view. Some cells may have images, and I add these images to cells through a UIImageView. These images react and open a new view controller if the user deletes it (and not the cell). Images have different sizes.

I added a UITapGestureRecognizer to the UIImageView, but it acts weirdly. I thought that the entire area of ​​the imageView would respond to gestures, but I see only some smaller areas, and in all images this area is located differently.

Here is the code from the init cell:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier UIImageView *thumbnailImageView = [[[UIImageView alloc] init] autorelease]; thumbnailImageView.tag = CELL_THUMBNAIL_TAG; thumbnailImageView.userInteractionEnabled = YES; [self.contentView addSubview:thumbnailImageView]; UITapGestureRecognizer *pictureTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePictureTap:)]; [thumbnailImageView addGestureRecognizer:pictureTap]; [pictureTap release]; 

All cells with images are displayed correctly in different orientations. The location of images and texts inside cells is performed using the layoutSubviews method:

 - (void)layoutSubviews if (thumbnail) { CGRect thumbnailFrame = CGRectMake(contentOrigin.x, contentOrigin.y, thumbnail.size.width, thumbnail.size.height); thumbnailImageView = (UIImageView *)[self.contentView viewWithTag:CELL_THUMBNAIL_TAG]; thumbnailImageView.frame = thumbnailFrame; thumbnailImageView.image = thumbnail; 

}

In tableView: cellForRowAtIndexPath: method I just pass all the necessary data to the cell, so the whole configuration goes to layoutSubviews

Could you help me determine why the area that responds to the click gesture is randomly determined and strange for all images, despite the fact that all the content is displayed correctly?

+4
source share
1 answer

After a long list of ideas, I finally found the reason for this strange behavior. This can help anyone with a similar problem.

In this project, I subclass UITableViewCell. Since all cells can have different sizes, I provide the correct height in the tableView: heightForRowAtIndexPath: method.

HOWEVER, in my usual cell initialization method, I did not set the proper autoresist mask. This caused the UITableViewCellContenView to have a constant height of 44pt. And this view is the supervisor for the entire contents of the cell.

I don’t know why, despite this, all the contents of the cell were displayed correctly and with the right sizes, but all the gestures reached the image only within this height of 44 pixels.

Customization

 self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

in the UITableViewCell custom initialization method, I solved this problem.

+4
source

Source: https://habr.com/ru/post/1402616/


All Articles