ios collectionView点击后只选中一个cell并改变样式


//按钮金额数组

@property (nonatomic,strong)NSArray *moneyArr;

//选中后添加的数组

@property (nonatomic,strong)NSMutableArray *rightLabelArray;



- (void)initCollectionView:(UIView *)veew {

    _moneyArr = @[@"10",@"2",@"8",@"66",@"8",@"9",@"1",@"18"];

    _rightLabelArray = [NSMutableArrayarray];

   //先实例化一个层

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayoutalloc ]init ];

   //创建一屏的视图大小

    UICollectionView *collectionView=[[UICollectionViewalloc ] initWithFrame :self.view.boundscollectionViewLayout:layout];

    collectionView.frame=CGRectMake(0,80,ScreenWidth,200);

    [collectionViewregisterClass:[UICollectionViewCellclass ] forCellWithReuseIdentifier :colletionCell];

    collectionView.backgroundColor = [UIColorclearColor];

    collectionView. delegate = self ;

    collectionView. dataSource = self ;

    [veew addSubview :collectionView];

}


//定义展示的UICollectionViewCell的个数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger )section {

    return _moneyArr.count ;

}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UINib *nib = [UINibnibWithNibName:@"llectionViewCell"bundle: [NSBundlemainBundle]];

    [collectionViewregisterNib:nibforCellWithReuseIdentifier:colletionCell];

    llectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:colletionCell forIndexPath:indexPath];

    cell.textLable.text =_moneyArr[indexPath.item];

    // 选中一个的方法

    if (_rightLabelArray.count ==0) {

        // 第一次加载的方法

        if (indexPath.row ==0 ) {

            cell.backgroundColor = [UIColorredColor];

        }else{

            cell.backgroundColor = [UIColorwhiteColor];

        }

    }else{

        if ([_rightLabelArray containsObject:_moneyArr[indexPath.item]]) {

            cell.backgroundColor = [UIColorredColor];

        } else {

            cell.backgroundColor = [UIColorwhiteColor];

        }

    }

    return cell;

}


//UICollectionView被选中时调用的方法

- ( void )collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"%zd",indexPath.row);

   //点击后添加因为只需要一个所以直接初始化掉重新赋值

    _rightLabelArray = [NSMutableArrayarrayWithObject:_moneyArr[indexPath.item]];

    [collectionView reloadData];

}


//定义每个UICollectionView的大小

- ( CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    returnCGSizeMake (70 ,70 );

}


//定义每个UICollectionView的边距

- ( UIEdgeInsets )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger )section {

    returnUIEdgeInsetsMake (10 , 10 ,10 ,10 );

}

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值