作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在重绘 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/
我是一名优秀的程序员,十分优秀!