ios -- UITableView简单的移动删除Demo

ios -- UITableView简单的移动删除Demo_第1张图片
Untitled.gif

当我们点击添加按钮时可以向提示框输入数据添加到表格,当我们点击编辑时我们可以对表格进行移动删除操作

#import "ViewController.h"

@interface ViewController (){
    
    UITableView *_table;
    
    NSMutableArray *marr;
    
    UIButton *leftBtn;
    
}

@end

@implementation ViewController
//这里主要实现表格的初始化,数组的初始化,左右导航按钮的初始化

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
    
    self.title = @"表格";
    
    //添加编辑按钮
    
    leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [leftBtn setTitle:@"编辑" forState:UIControlStateNormal];
    
    [leftBtn setTitle:@"完成" forState:UIControlStateSelected];
    
    leftBtn.frame = CGRectMake(0, 0, 80, 40);
    
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    //标题颜色
    
    [leftBtn setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
    
    [leftBtn addTarget:self action:@selector(leftBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:leftBtn];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarBtnClicked:)];
    
    //创建表格并初始化
    
    _table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    _table.delegate = self;
    
    _table.dataSource = self;
    
    //将表格添加到视图
    
    [self.view addSubview:_table];
    
    //初始化三个人名
    
    marr = [NSMutableArray arrayWithObjects:@"王二",@"刘山",@"李四", nil];
    
}
#pragma mark - 导航按钮点击触发方法

- (void)leftBarBtnClicked:(id)sender {
    
    //设置tableview编辑状态
    
    BOOL flag = !_table.editing;
    
    [_table setEditing:flag animated:YES];
    
    leftBtn.selected = flag;
    
}




- (void)rightBarBtnClicked:(id)sender {
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"请输入"
                              
                                                        message:@"请输入姓名"
                              
                                                       delegate:self cancelButtonTitle:@"确定"
                              
                                              otherButtonTitles:@"取消", nil];
    
    [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    
    [alertView show];
    
}




#pragma mark  获得输入框里的值

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    
    if ([buttonTitle isEqualToString:@"确定"]){
        
        UITextField *tf=[alertView textFieldAtIndex:0];//获得输入框
        
        NSString * res = tf.text;//获得值
        
        NSLog(@"%@",res);
        
        [marr addObject:res];
        
        [_table reloadData];
        
    }
    
}
#pragma mark - 表格方法

#pragma mark 选中行

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    // 取消选中状态
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


//表格有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return marr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
        
    }
    
    //将人名添加到表格
    
    cell.textLabel.text = marr[indexPath.row];
    
    //    cell.accessoryType = UITableViewCellAccessoryNone;//cell没有任何的样式
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头,距离右边有十几像素;
    
    //    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右边有一个蓝色的圆形button;
    
    //    cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右边的形状是对号;
    // 设置右侧视图为自定义视图
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
    myButton.frame = CGRectMake(0, 0, 50, 30);
    [myButton setTitle:@"购买" forState:UIControlStateNormal];
    [myButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

    cell.accessoryView = myButton;
    return cell;
    
}
#pragma mark - 提交编辑操作

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    //只要实现这个方法,就实现了默认滑动删除!!!!!
    
    if (editingStyle != UITableViewCellEditingStyleDelete)
        
        return;
    
    //删除数据模型
    
    [marr removeObjectAtIndex:indexPath.row];
    
    [_table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    
}

#pragma mark - 移动操作

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // Return NO if you do not want the item to be re-orderable.
    
    return YES;
    
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    
    //起始位置
    
    NSInteger fromRow = fromIndexPath.row;
    
    //终止位置
    
    NSInteger toRow = toIndexPath.row;
    
    NSLog(@"%ld,%ld",fromRow,toRow);
    
    //先取出起始位置的数据
    
    NSString *fromContent = marr[fromRow];
    
    //把起始位置的数据插入终止位置
    
    [marr insertObject:fromContent atIndex:toRow];
    
    NSLog(@"%@",marr);
    
}

@end

你可能感兴趣的:(ios -- UITableView简单的移动删除Demo)