gpt4 book ai didi

iOS UICollectionView 更改所选项目的颜色

转载 作者:行者123 更新时间:2023-11-29 00:35:08 26 4
gpt4 key购买 nike

我想更改我的 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。我该如何解决这个问题?

最佳答案

试试这个,像这样改变你的cellForItemAtIndexPath

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell;

StanderdScoreCardPlayerCollectionViewCell * standardScoreCardPlayerCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StanderdScoreCardPlayerCollectionViewCell" forIndexPath:indexPath];


if(selectedPlayerIndex == indexPath.row)
{
standardScoreCardPlayerCollectionViewCell.outerView.backgroundColor = NAV_BAR_BARTINT_COLOR_GREEN;
}
else
{
standardScoreCardPlayerCollectionViewCell.outerView.backgroundColor = UIColorFromRGB(0xC9C9C9);
}

cell = standardScoreCardPlayerCollectionViewCell;
}
return cell;
}

didSelectItemAtIndexPath这样

- (void)collectionView:(UICollectionView *)collectionView       didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
selectedPlayerIndex = indexPath.row;

[self displayDataWithPlayer:selectedPlayerIndex andHole:selectedHoleIndex];

[collectionView reloadData];
}

也无需实现 didDeselectItemAtIndexPath 删除该方法

关于iOS UICollectionView 更改所选项目的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40782815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com