好吧,我将复杂的应用程序修剪成一个简单的版本,以便每个人都能理解。因此,首先我从服务器下载一个 zip 文件,将其解压缩并将其放入缓存目录文件夹中。 AppDelegate 中发生的所有事情都可以很好地解压缩和存储图像。在调用 tableviews veiwdidload 方法时,我调用了一个函数来查找 iPhone 缓存目录文件夹中的图像文件。找到成功的图像文件后,我取该图像的文件路径
.h
@property (nonatomic, strong) IBOutlet UIImageView *theImage;
.m
NSString * photo;
NSString * photoType = @"6F0C437D-1BB2-482B-BF11-39BCAB668E4C";
photo = [NSString stringWithFormat:@"BANNERLOGO%@.png", photoType];
NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString * cacheDir = [cachesPaths objectAtIndex:0];
cacheDir = [cacheDir stringByAppendingPathComponent:@"1D2EA159-A312-4FB5-8FE5-B95879B242BB"];
NSString * photoPath = [cacheDir stringByAppendingPathComponent:photo];
BOOL success = [fileManager fileExistsAtPath:photoPath];
if (!success) {
NSLog(@"Error!!");
}
UIImage *image = [UIImage imageWithContentsOfFile:photoPath];
[self.theImage setImage:image];
所以这是我的问题。我似乎无法弄清楚为什么我抓取的图像没有出现?有谁知道为什么会这样?奇怪的是,当我设置 tableview 单元格的颜色时,它会像我期望的那样变成灰色。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
//DoesNot
cell.bannerImageView.image = self.theImage.image;
//Shows up in cell
cell.backgroundColor = [UIColor colorWithRed:0.9f green:0.9f blue:0.9f alpha:1.0f];
return (UITableViewCell *)cell;
}
如果有帮助的话,这是我的自定义单元格类的代码。
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
CGRect contentRect = self.contentView.bounds;
contentRect.size.height = 56.0;
self.bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 56)];
self.bannerImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:bannerImageView];
}
return self;
}
感谢提前指导!
最佳答案
尝试设置 imageView 背景颜色(比如绿色),看看您是否有布局问题或图像加载问题。
关于ios - 自定义 UITableViewCell ImageView 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24663963/