iOS之UICollectionViewCell的点击改变背景图片的两种方法

方法一:在控制器里面进行修改:

代理方法:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DiscoverRechangeCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverRechangeCenterCell" forIndexPath:indexPath];
    
    cell.discoverRechangeCenterModel = self.sourceArr[indexPath.row];
    
    
    //默认选中效果
    if (indexPath == self.selectIndexPath) {

        cell.backgroundImgV.hidden = NO;

    } else {

        cell.backgroundImgV.hidden = YES;
    }
    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DiscoverRechangeCenterCell *cell = (DiscoverRechangeCenterCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    //选中之后的cell变颜色
    [self updateCellStatus:cell selected:NO];
    
    self.selectIndexPath = indexPath;
    [collectionView reloadData];
    
}

//取消选中操作
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DiscoverRechangeCenterCell *cell = (DiscoverRechangeCenterCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [self updateCellStatus:cell selected:YES];
}
// 改变cell的背景图片
-(void)updateCellStatus:(DiscoverRechangeCenterCell *)cell selected:(BOOL)selected{
    
    cell.backgroundImgV.hidden = selected;
    
}

方法二:在UICollectionViewCell中重写select方法实现

UICollectionViewCell中:
- (void)setSelected:(BOOL)selected {
    
    [super setSelected:selected];
    
    if (selected) {
        
        self.backgroundImgV.hidden = NO;
        
    } else {
        
        self.backgroundImgV.hidden = YES;
        
    }
    
}
控制器中:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DiscoverRechangeCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverRechangeCenterCell" forIndexPath:indexPath];
    
    cell.discoverRechangeCenterModel = self.sourceArr[indexPath.row];
    
    
    //默认选中效果
    if (indexPath == self.selectIndexPath) {

        cell.backgroundImgV.hidden = NO;

    } else {

        cell.backgroundImgV.hidden = YES;
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DiscoverRechangeCenterCell *cell = (DiscoverRechangeCenterCell *)[collectionView cellForItemAtIndexPath:indexPath];

    self.selectIndexPath = indexPath;

    [cell setSelected:YES];
    [collectionView reloadData];
    
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,460评论 18 399
  • 你没有欠什么,我从来不会逼朋友去做不想做的事,我有我自己的原则,我不想一辈子被人踩在脚下,你以为我是臭要饭的,我等...
    股民复仇者联盟阅读 1,112评论 0 0
  • 这座石磨,大约有三十年的历史了。今年春节前,回了一趟老家。它依然孤独而又执拗的沉默在岁月的深处。从我家院子里...
    陌上夫君阅读 3,899评论 6 2
  • 我有点恐高,却喜欢站在高处。可能还想着从高处往下看的时候能够在人群中看到那个期待已久的背影吧!从未想过真正的离别。...
    岛上鱼儿儿儿阅读 2,962评论 0 0
  • 人开始犯罪 这比死容易出英雄 不管是好的还是坏的 好人做了一件坏事 便要钉死在十字架上 而浪子回头却被大家赞扬 你...
    未明花香阅读 1,531评论 0 0

友情链接更多精彩内容