您的位置:首页 > 移动开发 > IOS开发

iOS tableView编辑删除(单选 多选)

2016-05-18 10:02 447 查看
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;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: