iOS tableView編輯刪除(單選 多選)
阿新 • • 發佈:2019-02-09
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;
}