简体   繁体   中英

UIImageView fails to show image

UIImage is not displaying an image. I've created a UIImageView custom class for table cells using:

imgView = [[UIImageView alloc]init];
frame= CGRectMake(boundsX+10 ,0, 50, 44);
imgView.frame = frame;

Then, I tried to load each cell and nothing. But, I am able to read the image url in the log messages.

NSURL *url = [NSURL URLWithString: [dict objectForKey:@"imageAdd"]];
NSData *data = [NSData dataWithContentsOfURL: url];
UIImage * image = [[UIImage alloc] initWithData:[[data copy] autorelease]];
NSLog (@"The image is located at the URL: %@", url);
cell.imgView.image = image; 
return cell;

Standard UITableViewCell already contains UIImageView that appears to the left to all your labels if its image is set. You can access it using imageView property:

cell.imageView.image = someImage;

If for some reason standard behavior does not suit your needs (note that you can customize properties of that standard image view) then you can add your own UIImageView to the cell as Aman suggested in his answer. But in that approach you'll have to manage cell's layout yourself (eg make sure that cell labels do not overlap image). And do not add subviews to the cell directly - add them to cell's contentView:

// DO NOT!
[cell addSubview:imageView]; 
// DO:
[cell.contentView addSubview:imageView];

As far as I can see from your code, you never added imgView as a subview of your cell. add the line:

[cell.contentView addSubview:imgView];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM