【发布时间】:2016-03-17 20:15:51
【问题描述】:
每次调用 table reloadData 方法时,我的 tableview 单元格中的图像都会闪烁。发生闪烁是因为每次重新加载表时都在下载图像。我该如何做到这一点,这样这张图片就不会每次都被下载,而只会下载一次?
这是 SelectStationViewController.m 中的 cellForRowAtIndexPath 代码。此类处理 tableView。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
StationRest * StationRest = [[CurrentUser sharedInstance].userStations objectAtIndex:indexPath.row];
StationListCell *cell= [[StationListCell alloc]initWithFrame:CGRectMake(0, 0, 375,88)];
cell.cellDelegate = self;
//This method below downloads the image into the cell.
[cell configureCellWithStationRest:StationRest forCellType:StationListCellTypeSelect];
return cell;
}
这是 StationListCell.m 中的代码,该类与单元相连。这是使用 AFNetworking 下载图像的位置。我可以将 GCD 与 [[NSData alloc] initWithContentsOfURL 方法一起使用,而不是使用 AFNetworking,但我仍然可以达到相同的结果。
-(void)configureCellWithStationRest:(StationRest *)stationRest forCellType:(StationListCellType) cellType{
NSURL *url = [NSURL URLWithString:stationRest.thumbURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[self.thumbButton setImage:responseObject forState:UIControlStateNormal];
[self.thumbButton setContentMode:UIViewContentModeScaleAspectFill];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DLog(@"Image error: %@", error);
}];
[requestOperation start];
}
【问题讨论】:
标签: ios objective-c uitableview caching