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

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

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

iOS 中UICollectionView中的选中和取消选中_第1张图片

思路是我用两个可变数组,一个用来装载这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);


}

你可能感兴趣的:(UI,获取cell)