我有一个集合视图,它应该显示来自服务器的大量图像集。我从服务器获取所有图像信息作为一个大的JSON请求,将它们变成我自己的照片对象(POPhoto
),然后将它们显示在集合中,如下所示。问题是如果我快速滚动,之前显示的某个单元格将会以之前的图像集进入视图,然后如果我停止滚动,我可以看到图像突然变为其他内容。
当单元格离开屏幕并重新出现在不同的位置时,AFNetworking似乎没有取消请求。因此,如果我滚动到我的照片的中间,然后回到顶部,顶部的单元格将首先显示他们最初的图像,但然后将切换到属于列表中间的其他照片。
我的PhotoCell课程:
-(void)setPhoto:(POPhoto *)p
{
self->photo = p;
p.delegate = self;
self.progressView.hidden = YES;
if (p.thumbnail != nil) {
self.imageView.image = p.thumbnail;
}
else if (p.thumbnailURLPath != nil)
{
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:p.thumbnailURLPath]];
UIImage* placeholder = [UIImage imageWithContentsOfFile:@"placeholder.png"];
__weak typeof(self) weakSelf = self;
[self.imageView setImageWithURLRequest:request
placeholderImage:placeholder
success:^(NSURLRequest* request, NSHTTPURLResponse* response, UIImage* image) {
weakSelf.imageView.image = image;
weakSelf.photo.thumbnail = image;
}
failure:nil];
}
}
我的收藏视图:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
POPhotoCell* photoCell = (POPhotoCell*)[self.collectionView dequeueReusableCellWithReuseIdentifier:kPhotoCellReuseIdentifier forIndexPath:indexPath];
if (photoCell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"POPhotoCell" owner:self options:nil];
photoCell = (POPhotoCell*)[topLevelObjects objectAtIndex:0];
}
photoCell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"polaroid1"]];
[photoCell setPhoto:self.photos[indexPath.row]];
return photoCell;
}