我正在制作我的UIcollectionviewcell以下载图像异步但只有最后一个图像来自最后一个单元格。以下是我的代码
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
self.collectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSMutableDictionary *temperoryDictionary = [[NSMutableDictionary alloc] initWithDictionary:[self.weatherArray objectAtIndex:indexPath.row]];
NSString *imageURL = [[[temperoryDictionary valueForKey:@"weatherIconUrl"] objectAtIndex:0] valueForKey:@"value"];
[self downloadImageWithURL:[NSURL URLWithString:imageURL] completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
self.collectionViewCell.weatherImage.image= image;
}
}];
return self.collectionViewCell;
}
- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (!error)
{
UIImage *imageAsync = [[UIImage alloc] initWithData:data];
completionBlock(YES,imageAsync);
} else {
completionBlock(NO,nil);
}
}];
}
我不知道自己犯了什么错误。
答案 0 :(得分:0)
self.collectionViewCell
只会引用一个单元格。将其替换为局部变量UICollectionViewCell * collectionViewCell
答案 1 :(得分:0)
您不应该需要指向最后一个单元格的指针,因为它很可能是表格在图像加载完成时要求的最后一个单元格。
相反,试试这个:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSInteger currentTag = indexPath.row;
cell.tag = currentTag;
NSMutableDictionary *temperoryDictionary = [[NSMutableDictionary alloc] initWithDictionary:[self.weatherArray objectAtIndex:indexPath.row]];
NSString *imageURL = [[[temperoryDictionary valueForKey:@"weatherIconUrl"] objectAtIndex:0] valueForKey:@"value"];
__weak __typeof(cell) weakCell = cell;
[self downloadImageWithURL:[NSURL URLWithString:imageURL] completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded && weakCell.tag == currentTag) {
weakCell.weatherImage.image = image;
}
}];
}
这会保留指向当前单元格的弱指针,当完成加载图像时,它将检查单元格的标记(indexPath.row)是否相同,并在这种情况下设置图像。
答案 2 :(得分:-1)
而不是self.collectionViewCell
,您应该使用__block UICollectionViewCell * cell
:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
__block UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSMutableDictionary *temperoryDictionary = [[NSMutableDictionary alloc] initWithDictionary:[self.weatherArray objectAtIndex:indexPath.row]];
NSString *imageURL = [[[temperoryDictionary valueForKey:@"weatherIconUrl"] objectAtIndex:0] valueForKey:@"value"];
[self downloadImageWithURL:[NSURL URLWithString:imageURL] completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
cell.weatherImage.image= image;
}
}];
return cell;
}
对于自定义单元格,请使用__block CustomClass * cell
。 CustomClass
是自定义单元格的类。