根据Collection View编程指南,应该处理单元格高光的视觉状态UICollectionViewDelegate.像这样:
- (void)collectionView:(PSUICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
[cell highlight];
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
[cell unhighlight];
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢这种方法的是它为委托添加了非常特定于单元格的逻辑.事实上,UICollectionViewCell通过highlighted财产独立管理其突出显示的状态.
那么,压倒一切setHighlighted:不是一个更清洁的解决方案吗?
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
if (highlighted) {
[self highlight];
} else {
[self unhighlight];
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法有没有缺点而不是委托方法?
我用a UICollectionview来显示很多自定义单元格(250或更少).
那些单元格有一个主图像和一些文本.由于图像必须从Internet下载,我使用外部库AsyncImageView来执行延迟加载.
但问题是细胞的可重复使用性能让我发疯.
当我滚动图像出现在错误的单元格中时.除了索引路径之外,如何向图像添加标记或其他内容以避免出现问题?
也许AsyncImageView有一个我忽略的问题的解决方案......
或者另一种选择是更好的选择?
任何线索?
提前致谢
编辑:我的代码的简化版本
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
CollectionComercioCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
if (cell == nil){
}
else{
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget: cell.myImage];
}
cell.myImage.imageURL = nil;
cell.myImage.image = nil;
cell.myImage.hidden = TRUE;
cell.myImage.imageURL = [[myArray objectAtIndex:indexPath.row] getLogoUrl];
cell.myText.text = [[myArray objectAtIndex:indexPath.row] getName];
cell.myImage.hidden = FALSE;
return cell;
Run Code Online (Sandbox Code Playgroud)
}
CustomCell.m
- (void)prepareForReuse
{
[super prepareForReuse];
self.myImage.image = nil;
}
Run Code Online (Sandbox Code Playgroud)