UITableviewcell中显示错误的图像

时间:2013-04-16 07:34:01

标签: iphone uitableview grand-central-dispatch

我正在使用GCD将图片从网络加载到我的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
}

请告诉我,我哪里出错了。

1 个答案:

答案 0 :(得分:1)

您没有使用cell个变量,而是在来自UIImageView的{​​{1}}上设置图片。永远不会在桌子的单元格上正确设置图像。