ios表格删除多选及全选

效果图

Untitled3.gif

代码



@property (strong, nonatomic) IBOutlet UIBarButtonItem *deleteBtn;

@property (strong, nonatomic) IBOutlet UIBarButtonItem *editBtn;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *allselect;



@property (nonatomic ,retain) NSMutableArray *dataArray;
@property (nonatomic, retain) NSMutableDictionary *deleteDic;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.translucent = NO;
    [self startView];
}
- (void)startView {
    self.dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
    self.deleteDic = [[NSMutableDictionary alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.editBtn.title = @"编辑";
    
    self.allselect.enabled = NO;
    self.allselect.title = @"";
    
}

#pragma mark -IBAction-

- (IBAction)editData:(id)sender {
    if ([_editBtn.title isEqualToString:@"编辑"]) {
        _editBtn.title = @"确定";
        [self.tableView setEditing:YES animated:YES];
        self.allselect.enabled = YES;
        self.allselect.title = @"全选";
    } else {
        _editBtn.title = @"编辑";
        [_deleteDic removeAllObjects];
        [self.tableView setEditing:NO animated:YES];
        self.allselect.enabled = NO;
        self.allselect.title = @"";
    }
}

- (IBAction)deleteAction:(id)sender {
    [_dataArray removeObjectsInArray:[_deleteDic allKeys]];
    
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:[_deleteDic allValues]] withRowAnimation:UITableViewRowAnimationFade];
    [_deleteDic removeAllObjects];
}

- (IBAction)allselect:(id)sender {
    for (int row=0; row<_dataArray.count; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        [_tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
        [_deleteDic setObject:indexPath forKey:[_dataArray objectAtIndex:indexPath.row]];
    }
    
}


#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [_dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
   
    
    // Configure the cell...
    cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}


#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"选中");
    if ([_editBtn.title isEqualToString:@"确定"]) {
        [_deleteDic setObject:indexPath forKey:[_dataArray objectAtIndex:indexPath.row]];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([_editBtn.title isEqualToString:@"确定"]) {
        [_deleteDic removeObjectForKey:[_dataArray objectAtIndex:indexPath.row]];
    }
    

这是我之前下载的别人的demo效果及代码觉得很不错

谢谢。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,523评论 25 708
  • 递归算法要求 每次调用在规模上都有所缩小; 相邻两次重复之间有紧密的联系,前一次要为后一次做准备(通常前一次的输出...
    任教主来也阅读 223评论 0 1
  • 昨晚听了一节有关电信诈骗的微课,老师用自己切身经历告诉我们如今信息安全状况令人担忧,人之所以上当受骗,更多是对官方...
    终身学习的细嗅蔷薇阅读 279评论 0 2
  • Hey,28 想和未来的自己打个招呼,今年相处得挺一般,而且我觉得咱们还不是很熟,客气点没错的啦ꉂ೭(˵¯̴͒ꇴ¯...
    清和晨曦阅读 248评论 0 0
  • 文 | 驼玲 1. 昨晚,老友发给我美国新总统女儿伊万卡·特朗普的演讲视频,并搁了“被刷屏了”四个字后闪人,也没有...
    玲歌阅读 848评论 2 2