viewDidload 添加
for (int i = 0; i <_reouseArray.count ; i++) {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setValue:@"NO" forKey:[NSString stringWithFormat:@"%d", i]];
[_contacts addObject:dictionary];
}
cellForRow中进行赋值
NSMutableDictionary *dictionary = [_contacts objectAtIndex:[indexPath row]];
if ([[dictionary objectForKey:keyForChecked] isEqualToString:@"NO"]) {
[dictionary setObject:@"NO" forKey:keyForChecked];
[_showMediaCell setChecked:NO];
}else {
[dic_material setObject:@"YES" forKey:keyForChecked];
[_showMediaCell setChecked:YES];
}
#define keyForChecked [NSString stringWithFormat:@"%ld", (long)indexPath.row]
<pre name="code" class="objc">didSelect方法获取单个点击/取消操作
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
NewShowCollectionViewCell *cell = (NewShowCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
dic_material = [_contacts objectAtIndex:[indexPath row]];
if ([[dic_material objectForKey:keyForChecked] isEqualToString:@"NO"] ) {// && [_editState isEqualToString:@"on"]
[dic_material setObject:@"YES" forKey:keyForChecked];
[cell setChecked:YES];
id addObject = [_reouseArray objectAtIndex:[indexPath row]];
[self.removeList addObject:addObject];
}else{
[dic_material setObject:@"NO" forKey:keyForChecked];
[cell setChecked:NO];
id removeObject = [_reouseArray objectAtIndex:[indexPath row]];
[self.removeList removeObject:removeObject];
}
}
// 全选操作
- (void)editAllTheDeleteItems {
if (is_editAll) {
for (int i = 0; i < self.reouseArray.count; i ++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
NewShowCollectionViewCell *cell = (NewShowCollectionViewCell*)[self.collectionView cellForItemAtIndexPath:indexPath];
NSUInteger row = [indexPath row];
NSMutableDictionary *dictionary = [_contacts objectAtIndex:row];
[dictionary setObject:@"YES" forKey:keyForChecked];
[cell setChecked:YES];
[self.removeList addObject:_reouseArray[i]];
is_editAll = NO;
[_editAllBtn setImage:[UIImage imageNamed:@"全选2"] forState:(UIControlStateNormal)];
}
}else{
for (int i = 0; i < self.reouseArray.count; i ++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
NewShowCollectionViewCell *cell = (NewShowCollectionViewCell*)[self.collectionView cellForItemAtIndexPath:indexPath];
NSUInteger row = [indexPath row];
NSMutableDictionary *dictionary = [_contacts objectAtIndex:row];
[dictionary setObject:@"NO" forKey:keyForChecked];
[cell setChecked:NO];
[self.removeList removeObject:_reouseArray[i]];
is_editAll = YES;
[_editAllBtn setImage:[UIImage imageNamed:@"全选1"] forState:(UIControlStateNormal)];
}
}
}
自定义cell中
<pre name="code" class="objc">- (void)setChecked:(BOOL)checked{
if (checked)
{
[_selectBtn setImage:[UIImage imageNamed:@"选择2"] forState:(UIControlStateNormal)];
}
else
{
[_selectBtn setImage:[UIImage imageNamed:@"选择1"] forState:(UIControlStateNormal)];
}
_checkedForSelect = checked;
}