为什么第一次加载不正确但在刷新时正确加载?

时间:2016-04-05 17:25:36

标签: ios objective-c uitableview

所以在我的上一个问题中,我想知道如何使它成为一个圆圈,但现在我也试图调整圆圈的大小。出于某种原因,圆圈不会显示为圆圈(它显示为正方形)。直到我把它拉下来刷新它然后才能正确地获得它们。

//-------------------------------------------------------------------------------------------------------------------------------------------------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------------------------------------------------------------------------
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    PFObject *group = groups[indexPath.row];
     //if(group[PF_GROUP_LOGO] != nil){
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ // go to a background thread to load the image and not interfere with the UI
       UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:group[PF_GROUP_LOGO]]]];
        dispatch_async(dispatch_get_main_queue(), ^{ // synchronize back to the main thread to update the UI with your loaded image

            cell.imageView.image = image;
            cell.imageView.image = ResizeImage(image, 60, 60, 1);
            cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height /2;
            cell.imageView.layer.masksToBounds = YES;
            cell.imageView.layer.borderWidth = 0;
            cell.textLabel.text = group[PF_GROUP_NAME];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%d users", (int) [group[PF_GROUP_MEMBERS] count]];
            cell.detailTextLabel.textColor = [UIColor lightGrayColor];

        });
    });
    return cell;

}

1 个答案:

答案 0 :(得分:1)

只要cellForRowAtIndexPath需要回收一个单元格进行显示,就会在主线程上调用

tableview

您开始在后台线程上加载图像并在主线程上完成后更新回收的单元格

事情是,你不能保证单元格在那一点上没有被回收 - 特别是如果在主线程上更新单元格之前滚动了tableview(或者即使{{1} }将在单元格上触发。)

如果您不想在加载图片之前设置单元格的样式,则应该在setNeedsDisplay中使用自定义prepareForReuse子类进行设置 - 这样,单元格在显示之前就已准备就绪