Untitled.gif
先來看一下用到的數(shù)據(jù)
- (void)initData{
_goodsArr = @[@"YSL", @"CHANEL", @"TF"];
_colorsArr = @[@"12號色", @"52號色", @"400號色"];
_payWaysArr = @[@"上個月工資", @"這個月工資", @"下個月"];
_titleArr = @[@"送女朋友的品牌",@"色號",@"支付方式"];
_allInfoArr = @[_goodsArr, _colorsArr, _payWaysArr];
}
把每一個section中的數(shù)據(jù) 添加到了一個數(shù)組allInfoArr中 下面會用到哦
enm... 祝愿所有的你們都有機會送.
首先 初始化collectionview時候 把allowsMultipleSelection屬性改為yes
_collectionView.allowsMultipleSelection = YES;
在cellForItemAtIndexPath方法中 默認選中一個cell 這里默認選中了第0個
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:INDENTIFIE forIndexPath:indexPath];
[collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.section] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
cell.nameStr = _allInfoArr[indexPath.section][indexPath.item];
return cell;
}
選中后觸發(fā)的方法didSelectItemAtIndexPath中
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//根據(jù)數(shù)據(jù) 把所有的都遍歷一次 如果是當前點的cell 選中她 如果不是 就不選中她嘍
for (NSInteger i = 0; i < [[_allInfoArr objectAtIndex:indexPath.section] count];i++) {
if (i == indexPath.item) {
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:indexPath.section] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
}else {
[self.collectionView deselectItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:indexPath.section] animated:YES];
}
}
}
不選中cell的情況時候 didDeselectItemAtIndexPath中
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
//如果點擊了當前已經(jīng)選中的cell 忽略她~
[collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.item inSection:indexPath.section] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
}