项目中有个商品规格选择如图:
![](https://img.haomeiwen.com/i182745/c07a807999857606.png)
其实原理就是实现collectionview 的多section 单选,通过
model
来管理cell
的选中状态,通过一个数组dataArray
来存储选中的cell
的IndexPath
,但是在实际操作中出现的问题是,在遍历dataArray
中又对dataArray
进行修改会造成crash
.下面是我是现实的代码:
/** 保存index */
if (weakself.indexArray.count >0) {
for (NSIndexPath *index in weakself.indexArray) {
if (cellIndexPath.section != index.section) {
[weakself.tempArray addObject:cellIndexPath];
}else if (cellIndexPath.section == index.section){
if (indexPath.row != index.row) {
//先移除再添加
[weakself.tempArray removeObject:index];
[weakself.tempArray addObject:cellIndexPath];
}
}
}
}else{
[weakself.tempArray addObject:cellIndexPath];
}
if (weakself.indexArray.count > 0) {
[weakself.indexArray removeAllObjects];
}
/** 数组去重 */
NSSet*set = [NSSet setWithArray:weakself.tempArray];
[weakself.indexArray addObjectsFromArray:[set allObjects]];
[weakself.baseCollectionView reloadData];
关键是定义一个
tempArr
,然后进行去重就得到了选中的IndexPath
数组。
网友评论