这是我在 StackOver flow 中的第 1 篇文章,我的英语不好。我是iOS的初学者,所以我有点慢。实际上我想从 URL 解析我的 5 个数据(包括图像)。我使用自定义 tableView 单元格。我的纯文本清晰地显示在自定义单元格(标签中)中,但图像没有。这就是为什么我使用“dispatch_queue_t”在特定的 ImageView 中加载图像的原因。当我运行模拟器时,图像会一排又一排完美地显示出来(徽标和背景),但是当我重置模拟器时会出现问题。重置后,每个单元格(共 5 个)加载一张图像(队列中的最后一张)。我在“viewDidLoad”中调用我的“parsingLoad”方法,以便它只在加载 viewController 时加载一次。谁能告诉我我做错了什么?
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FairListCustomCell";
FairListCustomCell *cell = (FairListCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLabelObject = [[NSBundle mainBundle] loadNibNamed:@"FairListCustomCell" owner:self options:nil];
for (id currentObject in topLabelObject)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (FairListCustomCell*) currentObject;
break;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
currentFair = [fairs objectAtIndex:indexPath.row];
cell.fairNameLabel.text =currentFair.FairName;
cell.fairLocationLabel.text =currentFair.FairLocation;
cell.bookmarkImageView.image=[UIImage imageNamed:@"Favorite.png"];
cell.bookmarkImageView.tag=indexPath.row;
dispatch_queue_t imageQueueFairLogo = dispatch_queue_create("com.queue", NULL);
dispatch_async(imageQueueFairLogo, ^
{
dispatch_retain(imageQueueFairLogo);
UIImage *imageFairLogo = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairLogo]]]];
dispatch_async(dispatch_get_main_queue(), ^
{
cell.fairLogoImageView.image = imageFairLogo;
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_release(imageQueueFairLogo);
#endif
});
});
dispatch_queue_t imageQueueFairCellBackGround = dispatch_queue_create("com.queue", NULL);
dispatch_async(imageQueueFairCellBackGround, ^
{
dispatch_retain(imageQueueFairCellBackGround);
UIImage *imageFairCellBackGround = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairCellBackGround]]]];
dispatch_async(dispatch_get_main_queue(), ^
{
cell.fairCellBackGround.image = imageFairCellBackGround;
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_release(imageQueueFairCellBackGround);
#endif
});
});
return cell;
}
我还尝试了另一种方法。第一个:- 对两个图像都使用“global_queue”(这里只是放一个示例),如下所示:
imageQueueFairCellBackGround = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(imageQueueFairCellBackGround, ^
{
dispatch_retain(imageQueueFairCellBackGround);
UIImage *imageFairCellBackGround = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairCellBackGround]]]];
dispatch_async(dispatch_get_main_queue(), ^
{
cell.fairCellBackGround.image = imageFairCellBackGround;
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_release(imageQueueFairCellBackGround);
#endif
});
});
return cell;
第二:-像这样:
dispatch_queue_t fairCellBackGroundcallerQueue = dispatch_get_current_queue();
dispatch_queue_t fairCellBackGrounddownloadQueue = dispatch_queue_create("Thumb downloader", NULL);
dispatch_async(fairCellBackGrounddownloadQueue, ^
{
UIImage *imageFairCellBackGround = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairCellBackGround]]]];
dispatch_async(fairCellBackGroundcallerQueue, ^
{
cell.fairCellBackGround.image = imageFairCellBackGround;
});
});
dispatch_release(fairCellBackGrounddownloadQueue);
但仍然没有解决。如果有人有任何解决方案,请与我分享。非常感谢,提前。