我正在为一个学校项目制作一个简单的 reddit 应用程序。我正在通过带有库的 json(例如http://www.reddit.com/.json )从 reddit 加载我的数据。
AFNetworking
我用一个原型单元格显示每个 reddit 线程,其中包含一个
UIImageView
用于帖子缩略图的单元格。我正在尝试使用
AFNetworking
该方法延迟加载图像setImageWithURLRequest
。
问题:当应用程序启动所有缩略图时,当我向下滚动 tableView 时,它们会延迟加载。一旦单元格离开视图并且我向上滚动到它,缩略图就会被占位符图像替换——即使它在滚动过去之前加载了正确的缩略图。
方法中的相关代码cellForRowAtIndexPath
。在setImageWithURLRequest
块 中调用延迟加载
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"threadCell";
SubredditThreadCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *tempDictionary = [self.subredditArrayFromAFNetworking objectAtIndex:indexPath.row];
NSDictionary *singleThreadDict = [tempDictionary objectForKey:@"data"];
if ( [[singleThreadDict objectForKey:@"thumbnail"] isEqualToString:@"nsfw"] ){
cell.postImageThumbnail.image = [UIImage imageNamed:@"NSFWIcon"];
}
else if ([[singleThreadDict objectForKey:@"thumbnail"] isEqualToString:@"self"]){
cell.postImageThumbnail.image = [UIImage imageNamed:@"selfIcon"];
}
else if ([[singleThreadDict objectForKey:@"thumbnail"] length] == 0){
cell.postImageThumbnail.image = [UIImage imageNamed:@"genericIcon"];
}
else{
NSURL *thumbURL = [NSURL URLWithString:[singleThreadDict objectForKey:@"thumbnail"] ];
[cell.postImageThumbnail setImageWithURLRequest:[NSURLRequest requestWithURL:thumbURL]
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
if (request) {
[UIView transitionWithView:cell.postImageThumbnail
duration:1.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[cell.postImageThumbnail setImage:image];
}
completion:NULL];
}
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
{
NSLog(@"failure loading thumbnail");
}
];
}
return cell;
}