我有一个UICollectionView
。当用户单击一个单元格时,该单元格的颜色将变为红色。我已经对上面的部分进行了编码,它工作得很完美。
但是,当我在下面滚动时,集合视图中的其他单元格已经用红色高亮显示。我确信它还没有被选中,而只是突出显示。
我该怎么解决这个问题。
我的代码如下:
cellForItemAtIndexPath法
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"cell";
ViewCell *vc = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
vc.vciv.image = [arrayOfImages objectAtIndex:indexPath.row];
return vc;
}
didSelectItemAtIndexPath法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if([items containsObject:allItem[indexPath.row]] ){
UICollectionViewCell *c =[collectionView cellForItemAtIndexPath:indexPath];
c.backgroundColor = [UIColor clearColor];
} else {
UICollectionViewCell *c =[collectionView cellForItemAtIndexPath:indexPath];
c.backgroundColor = [UIColor redColor];
}
发布于 2015-03-17 03:34:48
在重用集合视图单元时,需要确保在cellForItemAtIndexPath:
方法中不突出显示已退出队列的单元格:
UICollectionViewCell *vc = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
vc.backgroundColor = [UIColor clearColor];
此代码将始终在脱队列时清除单元格的背景。如果需要保留高亮显示,则需要存储要突出显示的单元格的indexPath
,并检查当前单元格是否在该列表中。
编辑
您的items
变量似乎非常适合这样做。所以你会做:
UICollectionViewCell *vc = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
vc.backgroundColor = [items containsObject:allItem[indexPath.row]] ? [UIColor redColor] : [UIColor clearColor];
https://stackoverflow.com/questions/29097893
复制