當我首次加載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];
});
}
}];
}];
}