UItableViewマルチロー削除
6324 ワード
自分のプロジェクトでUItableViewの複数行を使って削除するので、記録しておきます
//
// ViewController.m
// DeleteMoreCell
//
// Created by Rambo on 16/4/11.
// Copyright © 2016 deleteCell. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArrayM;
@property (nonatomic,strong) NSMutableArray *deleteDataArrayM;
@property (nonatomic,strong) UIBarButtonItem *rightItem;
@property (nonatomic,strong) UIBarButtonItem *leftItem;
@property (nonatomic,strong) UIButton *deleteBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.rightItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:@selector(editTableView:)];
self.navigationItem.rightBarButtonItem = self.rightItem;
self.leftItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:@selector(allSelectedTableViewCell:)];
[self createTableView];
}
- (void)createTableView{
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.editing = NO;
self.tableView.tableFooterView = [UIView new];
[self.view addSubview:self.tableView];
}
#pragma mark - UITableViewDelegate And UITabelViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArrayM.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@",self.dataArrayM[indexPath.row]];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.dataArrayM removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
// self.rightItem.title = @" ";
return @" "; //
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (!tableView.isEditing) {
return UITableViewCellEditingStyleDelete; //
}
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; //
}
/** cell */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView.isEditing) {
[self.deleteDataArrayM addObject:self.dataArrayM[indexPath.row]];
}
}
/** cell */
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView.isEditing) {
[self.deleteDataArrayM removeObject:self.dataArrayM[indexPath.row]];
}
}
#pragma mark - Action
/** cell */
- (void)editTableView:(UIBarButtonItem*)barBtn{
self.tableView.editing = NO; // ,
CGRect frame = self.tableView.frame;
if ([barBtn.title isEqualToString:@" "]) {//
self.tableView.editing = YES;
self.tableView.allowsMultipleSelection = YES;
frame.size.height -= self.deleteBtn.frame.size.height;
self.tableView.frame = frame;
self.deleteBtn.hidden = NO;
//
self.navigationItem.leftBarButtonItem = self.leftItem;
barBtn.title = @" "; // /
return;
}
self.tableView.editing = NO;
self.tableView.allowsMultipleSelection = NO;
frame.size.height += self.deleteBtn.frame.size.height;
self.tableView.frame = frame;
self.deleteBtn.hidden = YES;
self.navigationItem.leftBarButtonItem = nil; //
[self.deleteDataArrayM removeAllObjects]; // cell
barBtn.title = @" "; // /
}
/** cell */
- (void)allSelectedTableViewCell:(UIBarButtonItem*)barBtn{
[self.deleteDataArrayM removeAllObjects]; // cell
[self.deleteDataArrayM addObjectsFromArray:self.dataArrayM];
// cell
for (int i = 0; i < self.dataArrayM.count; i ++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
}
/** cell */
- (void)deleteCell{
[self.dataArrayM removeObjectsInArray:self.deleteDataArrayM];
[self.tableView reloadData];
}
#pragma mark - get
- (NSMutableArray *)dataArrayM{
if (!_dataArrayM) {
_dataArrayM = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
[_dataArrayM addObject:@(i)];
}
}
return _dataArrayM;
}
- (NSMutableArray *)deleteDataArrayM{
if (!_deleteDataArrayM) {
_deleteDataArrayM = [NSMutableArray array];
}
return _deleteDataArrayM;
}
- (UIButton *)deleteBtn{
if (!_deleteBtn) {
_deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame) - 44, self.view.frame.size.width, 44)];
[_deleteBtn setTitle:@" " forState:UIControlStateNormal];
_deleteBtn.backgroundColor = [UIColor orangeColor];
_deleteBtn.hidden = YES;
[self.view addSubview:_deleteBtn];
[_deleteBtn addTarget:self action:@selector(deleteCell) forControlEvents:UIControlEventTouchUpInside];
}
return _deleteBtn;
}
@end