您好,欢迎来到暴趣科技网。
搜索
您的当前位置:首页iOS 中UICollectionView中的选中和取消选中

iOS 中UICollectionView中的选中和取消选中

来源:暴趣科技网

iOS 中UICollectionView中的选中和取消选中

首先,我们要实现的效果图如图

思路是我用两个可变数组,一个用来装载这8个数据,另一个用来记录我所选中的话题,在点击完成操作时候,将所记录的话题数组上传至后台。接下来贴上代码:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    HotTopicsCollectionViewCell *cell = (HotTopicsCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    if(_choiseArray.count == 0){

        //如果选中的数组为空,则直接添加话题到数组中
    }else{

        for (int i=0; i<_choiseArray.count; i++) {

            if (_interestArray[indexPath.row][@"id"] == _choiseArray[i]) {

                [cell changeWhite];//将cell的颜色设置为未选中的颜色
                [_choiseArray removeObjectAtIndex:i];//从数组中删除选中的话题
                NSLog(@"_choise--%@", _choiseArray);
                return;//return
            }
        }
    }
    [_choiseArray addObject:_interestArray[indexPath.row][@"id"]];//添加到数组中

    [cell changeBlack];//设置选择状态的颜色

    NSLog(@"_choise--%@", _choiseArray);


}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoquwan.com 版权所有 湘ICP备2024080961号-7

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务