简体   繁体   English

UIImageView 无法显示图像

[英]UIImageView fails to show image

UIImage is not displaying an image. UIImage未显示图像。 I've created a UIImageView custom class for table cells using:我使用以下方法为表格单元格创建了一个UIImageView自定义类:

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.但是,我能够读取日志消息中的图像 url。

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.标准 UITableViewCell 已经包含 UIImageView,如果设置了它的图像,它会出现在所有标签的左侧。 You can access it using imageView property:您可以使用 imageView 属性访问它:

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.如果由于某种原因标准行为不适合您的需求(请注意,您可以自定义该标准图像视图的属性),那么您可以按照 Aman 在他的回答中建议的那样将自己的 UIImageView 添加到单元格中。 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:并且不要直接将子视图添加到单元格 - 将它们添加到单元格的 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.据我从您的代码中看到,您从未将 imgView 添加为单元格的子视图。 add the line:添加行:

[cell.contentView addSubview:imgView];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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