0

我正在使用 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
}

请让我知道,我哪里出错了。

4

1 回答 1

1

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

于 2013-04-16T09:58:07.483 回答