我的应用中有一个UITableView
,每个小区都会从互联网上提取图片。在拉动该图像时,我在那里有一个占位符图像(在InterfaceBuilder中设置)。但是,当我将UIImageView
设置为来自网络的新图像时,它只是覆盖旧图像而不是替换它。这可以很好,除非您实际上可以看到旧图像,因为两个图像都是aspect-fit
并且不必占用整个imageView
。
这是线程问题吗?有没有一种方法可以设置占位符图像,使其立即存在,甚至在cellForRowAtIndexPath
之前?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Get a new or recycled cell
RosterListingCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RosterCell" forIndexPath:indexPath];
LineListing *thisRosterListing = [self.rosters objectAtIndex:indexPath.row];
cell.playerNumberLabel.text = [NSString stringWithFormat:@"#%@",thisRosterListing.number];
cell.playerNameLabel.text = thisRosterListing.name;
cell.imageView.backgroundColor = [UIColor clearColor];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
UIImage *playerImage = [self.imageCache objectForKey:thisRosterListing.playerImageURL];
cell.imageView.image = playerImage;
if (playerImage == nil) {
NSURLSessionConfiguration *sessionConfig =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
thisRosterListing.playerImageURL = [thisRosterListing.playerImageURL stringByReplacingOccurrencesOfString:@"small" withString:@"medium"];
NSURLSessionDataTask *imageData = [session dataTaskWithURL:[NSURL URLWithString: thisRosterListing.playerImageURL]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle NSData
UIImage *image = [UIImage imageWithData:data];
thisRosterListing.image = image;
[self.imageCache setObject:image forKey:thisRosterListing.playerImageURL];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = image;
[self.tableView reloadData];
});
}];
[imageData resume];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
imageView.image = [UIImage imageNamed:@"indicator"];
cell.accessoryView = imageView;
cell.backgroundColor = [UIColor clearColor];
// set selection color
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = myBackView;
return cell;
}
答案 0 :(得分:2)
我认为你在两个不同的UIImageViews上设置图像。如果单击“Debug view hierachy”
当xcode运行时,底部工具栏上的
按钮你看到了什么。图像是两个不同的图像视图。我认为一个UIImageView不可能共享两个UIImages