Showing an image programmatically in tableview cell
I'm doing this to load an image into a tableview cell:
UIImage开发者_运维技巧 *image = [UIImage imageNamed:@"myimage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake( 0, 0, cell.frame.size.width, cell.frame.size.height );
imageView.contentMode = UIViewContentModeScaleToFill;
[cell addSubview:imageView];
[imageView release];
return cell;
However, no image displays as the cell's background. This is in a new navigation app. The above looks just like the steps in Interface Builder when you are using a custom cell. The image is in the app bundle. What am I doing wrong?
Are you trying to set the cell's background, or just add an image? If you're trying to set its background, you should use the cell.background property.
imageView.frame = cell.bounds;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.backgroundView = imageView;
精彩评论