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];
[_choiseArray removeObjectAtIndex:i];
NSLog(@"_choise--%@", _choiseArray);
return;
}
}
}
[_choiseArray addObject:_interestArray[indexPath.row][@"id"]];
[cell changeBlack];
NSLog(@"_choise--%@", _choiseArray);
}