gpt4 book ai didi

ios - UICollectionview 单元格重绘错误

转载 作者:行者123 更新时间:2023-11-29 13:00:41 26 4
gpt4 key购买 nike

我在重绘 uicollectionview 单元格的图像时遇到问题 - 图像是从从 JSON 解析的 URL 下载的。我有 10 个单元格,应用程序启动时只显示前 6 个,但未加载图像,当我向下滚动到其他单元格时,它们有它们的图像,当我滚动回顶部时,前 4 个单元格有它们的图像图像也是如此,只有单元格 5 和 6 没有重绘(它们一直可见)。我一直在尝试调试它很长时间但没有成功。

在 cellForItemAtIndexPath 中,我正在调用 SDWebImage(但这并不重要):

[cell.backgroundImage setImageWithURL:[NSURL URLWithString:timeSnap.thumbnailImageURL]  placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

但是当第一次调用 cellForItemAtIndexPath 时,timeSnap.thumbnailImageURL(其中 timeSnap 实体是单元模型)尚未初始化,因此它为空

在 timeSnap.thumbnailImageURL 初始化后我有:

dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});

编辑1:添加我的代码,它有点复杂 - 首先我将从服务器 API 获取 JSON,其中包含要在 Collection View 中显示的项目,然后对于每个项目,我必须从服务器 API 获取另一个 JSON,我可以从中为 timeSnap 构建 URL。缩略图图片网址

viewController类:

- (void)viewDidLoad
{
[super viewDidLoad];
[self refreshNewTimeSnapsFeed];
}

- (void)refreshNewTimeSnapsFeed {
Adapter *adapter = [AppDelegate instance].adapter;

[adapter getNewTimeSnapsWithCompletion:^(NSArray* jsonResponse) {
[self newTimeSnapsFeedRefreshed:jsonResponse];
}];
}

- (void)newTimeSnapsFeedRefreshed:(NSArray*)jsonResponse {
Adapter *adapter = [AppDelegate instance].adapter;

for (NSDictionary *timeSnapDict in jsonResponse) {
TimeSnap *timeSnap = [[TimeSnap alloc] initWithJSON:timeSnapDict];
[adapter getTimeSnapSnapsforId:timeSnap.id withCompletion:^(NSArray* jsonResponse) {
[timeSnap getSnapsJSON:jsonResponse];
[timeSnap getTimeSnapThumbnailImageURL];
}];

[self.timeSnaps addObject:timeSnap];
}

dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

TimeSnapCell *cell = (TimeSnapCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"TimeSnapCell" forIndexPath:indexPath];

TimeSnap *timeSnap = self.timeSnaps[indexPath.row];

cell.label.text = timeSnap.name;

[cell.backgroundImage setImageWithURL:[NSURL URLWithString:timeSnap.thumbnailImageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

return cell;
}

适配器类:

- (void)getNewTimeSnapsWithCompletion:(void (^)(NSArray* jsonResponse))completion {

NSString *urlAsString = [NSString stringWithFormat:@"..."];
NSURL *url = [[NSURL alloc] initWithString:urlAsString];

[self getJSONWithCompletion:completion fromURL:url forKey:@"Items"];
}

- (void)getTimeSnapSnapsforId:(NSNumber*)id withCompletion:(void (^)(NSArray* jsonResponse))completion {

NSString *urlAsString = [NSString stringWithFormat:@"..."];
NSURL *url = [[NSURL alloc] initWithString:urlAsString];

[self getJSONWithCompletion:completion fromURL:url forKey:@"Items"];
}

- (void)getJSONWithCompletion:(void (^)(NSArray* jsonResponse))completion fromURL:(NSURL*)url forKey:(NSString*)key {

//code for getting JSON from server API, then parsing into NSArray *items

dispatch_async(dispatch_get_main_queue(), ^{
if(completion){
completion(items);
}
});
}

最佳答案

当第二个 JSON 响应返回时,您需要重新加载单元格:

for (NSDictionary *timeSnapDict in jsonResponse) {
TimeSnap *timeSnap = [[TimeSnap alloc] initWithJSON:timeSnapDict];

NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.timeSnaps.count inSection:0];
[self.timeSnaps addObject:timeSnap];
[adapter getTimeSnapSnapsforId:timeSnap.id withCompletion:^(NSArray* jsonResponse) {
[timeSnap getSnapsJSON:jsonResponse];
[timeSnap getTimeSnapThumbnailImageURL];
[self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
}];
}

您的适配器类在主队列上执行完成 block ,因此您不需要在此处使用 dispatch_async(除非您不考虑适配器契约(Contract)的那一部分)。

关于ios - UICollectionview 单元格重绘错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19869527/

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