In my case, I used my own subclass inherited from UITableViewCell . It is very clean and easy to use. In addition, I could use different image sizes to resize this cell well. The point is to use an additional property that will replace the regular imageView
1: In a subclass, I added the mainImageView property, which can be used instead of imageView .
@property (nonatomic, retain) UIImageView *mainImageView;
2: Then in - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier . I selected and initialized mainImageView . You can set any frame (straight line) for mainImageView . Add it as a subtitle to the contentView . I used insetImageView to align it vertically in the middle of the `contentView '.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) {
3: override - (void)layoutSubviews to make sure other properties, such as textLabel , detailTextLabel , establish that their frames are well positioned with the mainImageView property mainImageView
- (void)layoutSubviews { [super layoutSubviews]; CGRect frameImageView = frameCellMainImageView; frameImageView.origin.x = insetImageView; frameImageView.origin.y = insetImageView; [self.mainImageView setFrame:frameImageView]; // Use changed frame frameImageView = self.mainImageView.frame; CGFloat newLeftInset = frameImageView.size.width + insetImageView; CGRect frameTextLabel = self.textLabel.frame; CGRect frameDetailLabel = self.detailTextLabel.frame; frameTextLabel.origin.x += newLeftInset; frameDetailLabel.origin.x += newLeftInset; CGFloat newTextWidth = 320.0; newTextWidth -= newLeftInset; newTextWidth -= insetImageView; newTextWidth -= insetImageView; frameTextLabel.size.width = newTextWidth; frameDetailLabel.size.width = newTextWidth; [self.textLabel setFrame:frameTextLabel]; [self.detailTextLabel setFrame:frameDetailLabel]; }
4: When using this cell in UITableDataSourceDelegate methods UITableDataSourceDelegate use cell.mainImageView to receive messages instead of the usual cell.imageView
source share