2016-11-24 106 views
0

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

+0

添加外出放屏幕短,如果你能 –

+0

我想你想管理这个像我们管理检查unCheck项目...对吗? –

+0

您需要在数组或字典中管理背景颜色的状态,并在'cellForItemAtIndexPath'中使用它。 –

回答

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去除方法

+0

是的,现在它的工作:)但是,为每个选择重新加载集合视图很好吗? –

+0

那么它将取决于您的要求,因为现在您只需设置单元格的背景色,在这种情况下,您也可以重新加载两个单元格,即先前选定的索引路径的第一个单元格和当前选定的索引路径的一个单元格 – Rajat