gpt4 book ai didi

ios - UITableView 异步图像加载 - 滚动之前不布局

转载 作者:行者123 更新时间:2023-11-29 03:06:18 25 4
gpt4 key购买 nike

当 tableView 首次加载时,我的异步加载图像没有加载到我的初始单元格中。它们仅在我将单元格滚动到屏幕外并再次将它们滚动到屏幕上后才会显示。所以我读了一些 S.O.关于这个问题的问题,我觉得我做的一切都是对的。这个方法中有很多事情要做,因为它用许多头像图像填充 UIView。

我在后台线程中检索图像,然后在设置图像时返回到主线程。然后我称之为:

[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];

});

}
}];
}];

}

最佳答案

您可能知道,单元格在首次出现在屏幕上时会被重新绘制。这就是为什么当它们重新出现时您会看到单元更新。完成更新后,您可以尝试强制刷新单元格。

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

关于ios - UITableView 异步图像加载 - 滚动之前不布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22740598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com