thewarri0r9 thewarri0r9 - 14 days ago 6
iOS Question

iOS UICollectionView change color of the selected item

I want to change the color of the selected item in my UICollectionView, also item that are not selected should have a default color. But sometimes there are more than two items are selected and sometimes none.

My code is:

- (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 always points to the selected player and it is declared 1 in viewDidLoad. How do i solve this?

Answer

Try this, change your cellForItemAtIndexPath like this

- (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;
}

and didSelectItemAtIndexPath like this

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

    [self displayDataWithPlayer:selectedPlayerIndex andHole:selectedHoleIndex];

    [collectionView reloadData];
}

also no need to implement didDeselectItemAtIndexPath remove that method