2016-11-24 18 views
0

UICollectionViewで選択した項目の色を変更したい、選択されていない項目もデフォルトの色にする必要があります。しかし時には、2つ以上の項目が選択されていることもあります。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を宣言しています。どのように私はこれを解決するのですか?

+0

アウトADDプット画面を削除didDeselectItemAtIndexPathを実装する必要はありませんようにこの

- (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などのcellForItemAtIndexPath

+0

私はあなたがuncheckチェック項目を管理するようにこれを管理したいと思う...右?? –

+0

配列や辞書の背景色の状態を管理し、 'cellForItemAtIndexPath'でそれを使う必要があります。 –

答えて

1

変更、これを試してみてください、あなたもこの

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

    [self displayDataWithPlayer:selectedPlayerIndex andHole:selectedHoleIndex]; 

    [collectionView reloadData]; 
} 

その方法あなたができればショート

+0

でその作業をする細胞を登録した:)しかし、それはすべての選択のためのコレクションビューをリロードするのは良いのですか? –

+0

これはあなたの要件に依存します。現在、セルの背景色を設定しています。この場合も、前に選択したindexpathの最初のセルと、現在選択されているindexpathの1つのセル – Rajat

関連する問題