我有一个问题
我有一个带有静态200个单元格的简单UICollectionView,可从Flickr加载图像。
我的CellForItemAtIndexPath看起来像这样:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"FlickrCell" forIndexPath:indexPath];
cell.backgroundColor = [self generateRandomUIColor];
if(![[cell.subviews objectAtIndex:0] isKindOfClass:[PFImageView class]])
{
NSURL *staticPhotoURL = [self.context photoSourceURLFromDictionary:[self.photos objectAtIndex:indexPath.row] size:OFFlickrSmallSize];
PFImageView *imageView = [[PFImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.height, cell.frame.size.width) andImageURL:staticPhotoURL andOwningCell:cell];
[cell addSubview:imageView];
}
return cell;
}
PFImageView是
UIImageView
的子类,它在后台线程上加载Flickr照片URL,然后在主线程上更新其自身的图像-正常工作。逻辑非常简单-如果没有一个可出队,我将创建一个单元。
如果单元格(我希望它已经出列并且已经具有PFImageView)没有具有PFImageView,则为该单元分配并初始化一个imageView并将其添加为该单元格的子视图。
因此,我希望如果单元已经出队,它应该已经有一个PFImageView作为子视图,并且因为我们不应该进入if语句来创建一个新的imageView并启动一个新的照片下载请求
相反,我看到的是UICollectionView顶部和底部的单元格暂时“不在屏幕上”-当它们重新显示在屏幕上时,它们并没有被重用,并且似乎创建了一个新的单元格并刷新了图片。
1)创建单元后,如何获得静态图像(即,当单元略微偏离屏幕时不刷新)。
2)为什么不重复使用细胞?
非常感谢您的宝贵时间。
约翰
最佳答案
我看到几个潜在的问题:
如果您的情况像您描述的那样简单,则可以将PFImageView添加到出列的UICollectionView的contentView属性中,从而摆脱困境。
在您的控制器中:
// solve problem 2
[self.collectionView registerClass:[UICollectionViewCell class] forReuseIdentifer:@"FlickrCell"];
在collectionView:cellForItemAtIndexPath中
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"FlickrCell" forIndexPath:indexPath];
cell.backgroundColor = [self generateRandomUIColor];
// solve problem 1 by looking in the contentView for your subview (and looping instead of assuming at 0)
PFImageView *pfImageView = nil;
for (UIView *subview in cell.contentView.subviews)
{
if ([subview isKindOfClass:[PFImageView class]])
{
pfImageView = (PFImageView *)subview;
break;
}
}
NSURL *staticPhotoURL = [self.context photoSourceURLFromDictionary:[self.photos objectAtIndex:indexPath.row] size:OFFlickrSmallSize];
if (pfImageView == nil)
{
// No PFImageView, create one
// note the use of contentView!
pfImageView = [[PFImageView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.height, cell.frame.size.width) andImageURL:staticPhotoURL andOwningCell:cell.contentView];
[cell.contentView addSubview:pfImageView];
}
else
{
// Already have recycled view.
// need to reset the url for the pfImageView. (Problem 3)
// not sure what PFImageView looks like so this is an e.g. I'd probably remove the
// URL loading from the ctr above and instead have a function that loads the
// image. Then, you could do this outside of the if, regardless of whether you had
// to alloc the child view or not.
[pfImageView loadImageWithUrl:staticPhotoURL];
// if you really only have 200 static images, you might consider caching all of them
}
return cell;
对于不太简单的情况(例如,我想在视觉上布置单元格,或在内容中有多个子级的情况),我通常使用Interface Builder自定义UICollectionViewCell。
@property (nonatomic, assign) IBOutlet UIImageView *image;
关于ios - 滚动期间重复使用UICollectionViewCells,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21614731/