Iphone UITableviewcell中显示了错误的图像

Iphone UITableviewcell中显示了错误的图像,iphone,uitableview,grand-central-dispatch,Iphone,Uitableview,Grand Central Dispatch,我正在使用GCD将图像从web加载到我的UITableViewCell。但是,出现了错误的图像 以下是代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; UITableVi

我正在使用GCD将图像从web加载到我的
UITableViewCell
。但是,出现了错误的图像

以下是代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier:@"newsTableCell"];
    NSString *user = [usernames objectAtIndex:[indexPath row]];
    NSString *stat = [statistics objectAtIndex:[indexPath row]];
    NSString *imagePath = [appDelegate.URL stringByAppendingString:@"ImageName"];
    UIImageView *userImageView = (UIImageView *)[self.view viewWithTag:80];


        NSString *imagePath1 = [imagePath stringByAppendingString:[imagepaths objectAtIndex:[indexPath row]]];

        NSURL *url = [NSURL URLWithString:imagePath1];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *img = [[UIImage alloc] initWithData:data];
        userImageView.layer.cornerRadius = 5.0;
        userImageView.layer.masksToBounds = YES;
         dispatch_sync(dispatch_get_main_queue(), ^{
        [userImageView setImage:img];
         });
    });
 return cell
}

请告诉我哪里出了问题。

您根本没有使用
单元格
变量,您正在
UIImageView
上设置图像,该视图来自
self.view
。图像永远不会正确设置在表格的单元格上。

谢谢您的回答。现在,图像显示正确,但仅在滚动时加载图像。这是建议的行为,移动设备的带宽和RAM受到限制,因此,仅因为显示了表格标题,用户下载数百张图片时就会受到影响。但是,如果您确实需要这样做,我建议您从单元格创建方法中检索图像(可能在ViewDidDisplay中)然后,只有在完成加载或滚动表格时,才将它们分配到可见单元格,并且新单元格将为已下载的图像排队。Ramu Pasupuleti能否请您详细说明解决问题的具体方法?因为我有完全一样的issue@Jonathan,我正在使用标签加载图像。所以,我用一个自定义的UITableViewCell类替换了它。哦,好吧,但是cell不会也解决这个问题吗?