UICollectionView延迟加载问题.CellForIndexPath在第一次加载时为零



所以我试图模仿苹果的LazyTableImages来制作我自己的LazyLoading UICollectionView。到目前为止,它一直在缓慢加载,但它从未在第一次加载时设置collectionview中单元格的imageview

这是我的cellForItem:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *woID = @"WO";
NSMutableArray *currentArr = [manager.dataDict objectForKey:manager.currentCategory.categoryName];
PaWallObject *object = [currentArr objectAtIndex:indexPath.item];
WOCollectionViewCell *cell = (WOCollectionViewCell *)[cv dequeueReusableCellWithReuseIdentifier:woID forIndexPath:indexPath];


if (currentArr.count > 0) {
cell.wallObject = object;
if (object.likesCount) cell.likesCountLabel.text = [NSString stringWithFormat:@"%@",object.likesCount];
else cell.likesCountLabel.text = @"0";
if (object.replyCount) cell.replyCountLabel.text = [NSString stringWithFormat:@"%@",object.replyCount];
else cell.replyCountLabel.text = @"0";
cell.postTimeLabel.text = [PaDataManager getTimeStr:object.createdAt];
AVFile *picFile = object.thumbnail;
if (picFile.isDataAvailable) {
cell.woimageview.image = [UIImage imageWithData:picFile.getData];
} else  {
if (!cv.decelerating && !cv.dragging) {
[self startLoading:object forIndexPath:indexPath];
}
cell.woimageview.image = [UIImage imageNamed:@"LipsCellBackground.png"];
}
//fit
[cell.likesCountLabel sizeToFit];
[cell.likesCountLabel setFrame:CGRectMake(cell.likeIcon.frame.origin.x + cell.likeIcon.frame.size.width + 3, 0, cell.likesCountLabel.frame.size.width, 20)];
[cell.replyIcon setFrame:CGRectMake(cell.likesCountLabel.frame.origin.x + cell.likesCountLabel.frame.size.width+5, 2, 16, 16)];
[cell.replyCountLabel sizeToFit];
[cell.replyCountLabel setFrame:CGRectMake(cell.replyIcon.frame.origin.x + cell.replyIcon.frame.size.width + 3, 0, cell.replyCountLabel.frame.size.height, 20)];
}

return cell;

}

因此在第一负载时,cv既不拖动也不减速。调用[self-startloading]并开始加载文件。这是开始加载的代码

- (void)startLoading:(PaWallObject *)wallobject forIndexPath:(NSIndexPath *)indexpath {
AVFile *fileToDownload = [filesInDownload objectForKey:indexpath];
if (fileToDownload == nil)
{
fileToDownload = wallobject.thumbnail;
[filesInDownload setObject:fileToDownload forKey:indexpath];
[fileToDownload getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
WOCollectionViewCell *cell = (WOCollectionViewCell *)[self.cv cellForItemAtIndexPath:indexpath];
NSLog(@"load completed, cell is%@", cell);
cell.woimageview.image = [UIImage imageWithData:data];
[filesInDownload removeObjectForKey:indexpath];
}
}];
}

}

正如苹果公司的示例项目所指示的,当cv停止拖动和减速时,就会调用[self loadImagesForOnScreenItems]。然而,在第一次加载时,不能有任何触摸输入,所以我认为这种方法与我的问题无关。

我研究了前面提到的- (void)startLoading:(PaWallObject *)wallobject forIndexPath:(NSIndexPath *)indexpath;方法,发现当文件在第一次加载时,在完成块中完成加载时,WOCollectionViewCell *cell = (WOCollectionViewCell *)[self.cv cellForItemAtIndexPath:indexpath];将始终为零。因此加载的数据无法跟踪cell.woimageview.image,因为该单元格根本不存在。然而,在这一轮之后,当我向下滚动列表时,单元格开始有值。

为什么?当从cellForIndexPath调用[self startloading:object forIndexPath:indexPath]时,单元格不是已经生成了吗?如果没有,有人能为这个问题提供解决方案吗?

cellForItem...数据源方法返回单元格之前,集合视图没有用于该索引路径的单元格,因此它在startLoading方法中返回nil,因为这是在数据源方法回归之前调用的。

将单元格作为附加参数传递给startLoading将解决此问题。

最新更新