2013-10-24 182 views
5

我正在iOS中构建应用程序,我希望CollectionView中的单元格在触摸时突出显示,与常规按钮非常相似。我如何在didSelectItemAtIndexPath中实现这一点:(NSIndexPath *)indexPath方法?突出显示CollectionView中的单元格

感谢

+0

改变那个细胞的背景颜色。 – user1673099

回答

7

尝试是这样的:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    ..... 
    if (cell.selected) { 
     cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; // highlight selection 
    } 
    else 
    { 
     cell.backgroundColor = [UIColor clearColor]; // Default color 
    } 
    return cell; 
} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; //  //cell.lblImgTitle.text = @"xxx"; 
} 

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor clearColor]; 
} 
+0

我通过更改didHighlightItemAtIndexPath和didUnhighlightItemAtIndexPath方法中单元格中的图片的Alpha来解决此问题。无论如何,我会把你的答案设定为正确答案。谢谢。 –

+1

你为什么不发布你在答案中如何解决你的问题?这将有助于其他人... diogo-appdev –

0

试试这个

cell.selectedBackgroundView.backgroundColor = [UIColor greenColor]; 
5

如果你的子类的细胞类,把这个在您的.m文件

- (void)setSelected:(BOOL)selected 
{ 
    if(selected) 
    { 
     self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5]; 
    } 
    else 
    { 
     self.backgroundColor = [UIColor whiteColor]; 
    } 
} 
0

为什么不能改变通过可可豆荚的背景颜色?我新增了一个用户自定义的collectionView细胞分类号'

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 
     CHSMenuControlCell *cell = (CHSMenuControlCell*)[collectionView cellForItemAtIndexPath:indexPath]; 
     cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; //  //cell.lblImgTitle.text = @"xxx"; 
    } 

    - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    CHSMenuControlCell *cell = (CHSMenuControlCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor clearColor]; 
} 
相关问题