Possible Duplicate:
Changing bounds of imageView of UITableViewCell
I have a custom UITableViewController
, and within I am dynamically styling a UITableViewCell
with style UITableViewCellStyleSubtitle
. All is good, except that when a cell is re-used, its imageView
property does not respect my contentMode
or clipsToBounds
settings. Ideally, I would prefer the content to adhere to the default size/shape (which I believe is 40x40 square).
There are other posts (links below) that suggest subclassing UITableViewCell
, or re-sizing your images to fit exactly, but both these feel like workarounds and (imho) would overly-complicate my code given that I am already pulling small (non-square) images.
Suggests subclassing UITableViewCell: Changing bounds of imageView of UITableViewCell
Suggests resizing all images: UITableViewCell's imageView fit to 40x40
Example code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *kCellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier];
}
NSString *mainText = @"mainText";
NSString *subtitle = @"subText";
[[cell textLabel] setText:mainText];
[[cell detailTextLabel] setText:subtitle];
// get album cover
UIImage *imageForCell = [album objectForKey:@"coverImage"];
// this is the broken part -
// it works upon first load, but it is NOT applied when a cell is re-used/dequeued..
// instead, the image is displayed un-scaled and un-square
[[cell imageView] setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
[[cell imageView] setContentMode:UIViewContentModeScaleAspectFill];
[[cell imageView] setClipsToBounds:YES];
if (imageForCell) {
[[cell imageView] setImage:imageForCell];
} else {
/* code to retrieve custom image */
}
return cell;
}
What gives? Is there a bug in UITableViewCell? Or is this for some reason by design?