【发布时间】:2015-07-20 14:41:22
【问题描述】:
我的应用程序中有一个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;
}
【问题讨论】:
-
能否请您发布更新图像的代码部分?
UIImageView不能同时显示两个图像,所以我猜你是在视图层次结构中添加多个图像视图。 -
您确定始终将图像设置在同一个图像视图上,而不是每次重复使用单元格时都添加额外的图像视图吗?
-
好的,我添加了我的
cellForRowAtIndexPath method -
您可以尝试使用自动布局解决此问题。事实上,这可能是一个相关问题。
-
你指的是什么自动布局问题?
标签: ios uitableview uiimageview uiimage