2014-03-30 55 views
0

當我首次加載tableView時,異步加載的圖像不會加載到我的初始單元格中。他們只在我將屏幕滾動到屏幕後再顯示在屏幕上後才顯示。所以我讀過一些S.O.在這個問題上的問題,我覺得我做的一切都是正確的。這個方法有很多,因爲它用一些頭像圖像填充UIView。UITableView異步圖像加載 - 直到滾動才能佈局

我在後臺線程中檢索圖像,然後當我設置圖像時,我會回到主線程。然後我打電話這樣的:

[correctCell setNeedsLayout]; 

在我看來,我做的一切正確的,但它似乎像setNeedsLayout只是沒有接到回訪電話的主線程上。

下面是該我從tableView:cellForRowAtIndexPath:調用方法:

- (void) setPeopleAvatars:(NSArray*)people cell:(WSExploreTableViewCell*)cell indexPath:(NSIndexPath *) indexPath{ 

    [cell clearPeopleAvatars]; 


    // Dimensions 
    CGFloat maxAvatars = cell.peopleView.frame.size.width/(kWSExploreCellPeopleAvatarDim + kWSExploreCellPeopleAvatarPadding); 
    CGFloat peopleAvatarX = cell.peopleView.frame.size.width - kWSExploreCellConvoBubblePadding - kWSExploreCellPeopleAvatarDim; 
    CGFloat peopleAvatarY = cell.peopleView.frame.size.height/2 - kWSExploreCellPeopleAvatarDim/2; 

    __block CGFloat lastAvatarX; 
    __block NSUInteger numAvatars = 0; 


    // First set the convo bubble image 
    cell.convoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kWSExploreCellConvoBubbleIcon]]; 
    cell.convoImageView.frame = CGRectMake(cell.peopleView.frame.size.width - 1.0f - cell.convoImageView.frame.size.width, 
             cell.peopleView.frame.size.height/2 - cell.convoImageView.frame.size.height/2, 
             cell.convoImageView.frame.size.width, 
             cell.convoImageView.frame.size.height); 

    [cell.peopleView addSubview:cell.convoImageView]; 
    cell.convoImageView.hidden = YES; 


    if (people.count == 0) { 
     cell.convoImageView.hidden = NO; 

    } 


    [people enumerateObjectsUsingBlock:^(id person, NSUInteger idx, BOOL *stop) { 


     // Stop at maxAvatars 
     if (idx >= maxAvatars) { 
      *stop = YES; 
      return; 
     } 

     [WSDatabaseManager getSmallProfilePic:(PFUser*)person callback:^(UIImage *image, NSError *error) { 

      if (!error) { 

       dispatch_async(dispatch_get_main_queue(), ^{ 

        WSExploreTableViewCell * correctCell = (WSExploreTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath]; 

        WSProfileImageView* avatarImageView = [[WSProfileImageView alloc] initWithImage:image croppedToCircle:YES diameter:kWSExploreCellPosterAvatarDim]; 
        [avatarImageView setBackgroundColor:[UIColor clearColor]]; 
        [avatarImageView setOpaque:NO]; 
        [correctCell.peopleView addSubview:avatarImageView]; 


        // Layout 
        CGFloat totalAvatarWidth = kWSExploreCellPeopleAvatarDim + kWSExploreCellPeopleAvatarPadding; 
        lastAvatarX = peopleAvatarX - (numAvatars * totalAvatarWidth); 

        [avatarImageView setFrame:CGRectMake(lastAvatarX, peopleAvatarY, kWSExploreCellPeopleAvatarDim, kWSExploreCellPeopleAvatarDim)]; 


        // Move the convoImageView 
        CGRect convoFrame = correctCell.convoImageView.frame; 
        convoFrame.origin.x = convoFrame.origin.x - totalAvatarWidth; 
        correctCell.convoImageView.frame = convoFrame; 

        correctCell.convoImageView.hidden = NO; 



        // Update Counter 
        numAvatars++; 

        [correctCell setNeedsLayout]; 

       }); 

      } 
     }]; 
    }]; 

} 

回答

1

正如你可能知道,當它第一次出現在屏幕上的細胞被重新繪製。這就是爲什麼當他們重新出現時你會看到單元格更新。完成更新後,您可以嘗試強制刷新單元格。

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 
0

嘗試SDWebImage

很會照顧時,他們被檢索,並且還將快取載入圖像。所有你需要做的是進口的UIImageView + WebCache.h

[convoImageView setImageWithURL:YOUR_IMAGE_URL_HERE placeholderImage:[UIImage的imageNamed:@ 「placeholder.png」] 完成:^(* UIImage的圖像,NSError *錯誤,SDImageCacheType cacheType){

//做完成塊的東西

      }];