问题描述
选择单元格后,我想处理更改单元格的外观.我想出了委托方法 collectionView:didSelectItemAtIndexPath:
&collectionView:didDeselectItemAtIndexPath:
是我应该编辑单元格的地方.
Upon cell selection, I want to handle changing the cell appearance. I figured the delegate method collectionView:didSelectItemAtIndexPath:
& collectionView:didDeselectItemAtIndexPath:
is where I should edit the cell.
-(void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
DatasetCell *datasetCell =
(DatasetCell *)[collectionView cellForItemAtIndexPath:indexPath];
[datasetCell replaceHeaderGradientWith:[UIColor skyBlueHeaderGradient]];
datasetCell.backgroundColor = [UIColor skyBlueColor];
}
和
-(void)collectionView:(UICollectionView *)collectionView
didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
DatasetCell *datasetCell =
(DatasetCell *)[collectionView cellForItemAtIndexPath:indexPath];
[datasetCell replaceHeaderGradientWith:[UIColor grayGradient]];
datasetCell.backgroundColor = [UIColor myDarkGrayColor];
}
这很好用,除非单元格被重复使用.如果我选择索引 (0, 0) 处的单元格,它会改变外观,但是当我向下滚动时,另一个单元格处于选定状态.
This works fine, except when the cell gets reused. If I select cell at index (0, 0), it changes the appearance but when I scroll down, there is another cell in the selected state.
我相信我应该使用 UICollectionViewCell
方法 -(void)prepareForReuse
准备单元格以供重用(即,将单元格外观设置为非选定状态),但它的给我带来困难.
I believe I should use the UICollectionViewCell
method -(void)prepareForReuse
to prep the cell for resuse (ie, set the cell appearance to non selected state) but its giving me difficulties.
-(void)prepareForReuse {
if ( self.selected ) {
[self replaceHeaderGradientWith:[UIColor skyBlueHeaderGradient]];
self.backgroundColor = [UIColor skyBlueColor];
} else {
[self replaceHeaderGradientWith:[UIColor grayGradient]];
self.backgroundColor = [UIColor myDarkGrayColor];
}
}
当我滚动回顶部时,索引 (0, 0) 处的单元格处于取消选中状态.
When I scroll back to the top, the cell at index (0, 0) is in the deselected state.
当我刚刚使用 cell.backgroundView 属性时,为了防止这种情况发生是:
When I just used the cell.backgroundView property, to prevent this from happening was to:
-(void)prepareForReuse {
self.selected = FALSE;
}
并且选择状态按预期工作.
and the selection state worked as intended.
有什么想法吗?
推荐答案
你的观察是正确的.这种行为是由于单元格的重用而发生的.但是您不必对 prepareForReuse 做任何事情.而是检查 cellForItem 并相应地设置属性.有点像..
Your observation is correct. This behavior is happening due to the reuse of cells. But you dont have to do any thing with the prepareForReuse. Instead do your check in cellForItem and set the properties accordingly. Some thing like..
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvCell" forIndexPath:indexPath];
if (cell.selected) {
cell.backgroundColor = [UIColor blueColor]; // highlight selection
}
else
{
cell.backgroundColor = [UIColor redColor]; // Default color
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
datasetCell.backgroundColor = [UIColor blueColor]; // highlight selection
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
datasetCell.backgroundColor = [UIColor redColor]; // Default color
}
这篇关于UICollectionView 单元格选择和单元格重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!