我想更改我的UICollectionView中选定项目的颜色,未选择的项目也应具有默认颜色。但有时会有超过两个项目被选中,有时甚至没有。iOS UICollectionView更改所选项目的颜色
我的代码是:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell;
StanderdScoreCardPlayerCollectionViewCell * standardScoreCardPlayerCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StanderdScoreCardPlayerCollectionViewCell" forIndexPath:indexPath];
if(standardScoreCardPlayerCollectionViewCell.selected || selectedPlayerIndex == indexPath.row){
standardScoreCardPlayerCollectionViewCell.outerView.backgroundColor = NAV_BAR_BARTINT_COLOR_GREEN;
}
else{
standardScoreCardPlayerCollectionViewCell.outerView.backgroundColor = UIColorFromRGB(0xC9C9C9);
}
cell = standardScoreCardPlayerCollectionViewCell;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell = [collectionView cellForItemAtIndexPath:indexPath];
StanderdScoreCardPlayerCollectionViewCell * cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.outerView.backgroundColor = NAV_BAR_BARTINT_COLOR_GREEN;
selectedPlayerIndex = indexPath.row;
[self displayDataWithPlayer:selectedPlayerIndex andHole:selectedHoleIndex];
}
- (void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
StanderdScoreCardPlayerCollectionViewCell * cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.outerView.backgroundColor = UIColorFromRGB(0xC9C9C9);
}
selectedPlayerIndex
始终指向选定的球员,它是在viewDidLoad中宣布1。我如何解决这个问题?
添加外出放屏幕短,如果你能 –
我想你想管理这个像我们管理检查unCheck项目...对吗? –
您需要在数组或字典中管理背景颜色的状态,并在'cellForItemAtIndexPath'中使用它。 –