繁体   English   中英

在CollectionView中突出显示单元格

[英]Highlight cells in CollectionView

我正在iOS中构建一个应用程序,我希望我的CollectionView中的单元格在触摸时突出显示,就像普通按钮一样。 如何在didSelectItemAtIndexPath:(NSIndexPath *)indexPath方法中实现此目的

尝试这样的事情:

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

如果您将单元类子类化,请将其放在.m文件中

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

尝试这个

cell.selectedBackgroundView.backgroundColor = [UIColor greenColor];

为什么不能通过可可豆荚改变背景颜色?我新增了一个用户定义的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];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM