编辑表格@tableView实现增加、删除、移动的操作

首先做好准备工作,在storyboard中做好工具条和tableView的准备工作,然后进行以下操作。。。看代码可知具体操作:


//=========================================
//要想实现增加删除操作,必须做到这三部,
//1、self.table.editing 即编辑状态要打开。为YES
//2、实现方法:-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//<即返回单元格的编辑状态>
//3、实现方法-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//<即进行删除增加操作>
//=========================================
//=============================
//实现移动的操作只需两步
//1、把编辑状态打开
//2、进行以下方法:-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

//=============================

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    //记录当前正在执行的操作,0代表删除,1代表增加
    NSInteger action;
    NSMutableArray *list;
}
//下面是工具栏的两个按钮,具体看图可知
@property (strong, nonatomic) IBOutlet UIBarButtonItem *addbtn;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *deletebtn;

@property (strong, nonatomic) IBOutlet UITableView *table;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    list = [[NSMutableArray alloc]initWithObjects:@"孙悟空",@"猪八戒",@"唐僧",@"狐狸精",@"蜘蛛精" ,@"小猫",@"小猪",@"小狗",nil];
    action = 0;
    self.table.delegate = self;
    self.table.dataSource = self;

    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return list.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID =  @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSInteger rowOd = indexPath.row;
    cell.textLabel.text = [list objectAtIndex:rowOd];
    return cell;
}
//该方法的返回值作为删除指定表格行时确定按钮的文本,如图1
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
//=========================================
//要想实现增加删除操作,必须做到这三部,
//1、self.table.editing 即编辑状态要打开。为YES
//2、实现方法:-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//<即返回单元格的编辑状态>
//3、实现方法-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//<即进行删除增加操作>
//=========================================
//该方法是uitableviewdelegate协议中的方法,该方法的返回值决定单元格的编辑状态。

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //如果action的值为0,代表将要删除
    return action == 0 ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}
//
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [list removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }else
    {
        [list insertObject:[list objectAtIndex:indexPath.row] atIndex:indexPath.row+1];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
//uitableviewdatasource协议中定义的方法,该方法的返回值决定某行是否可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    //第五个表格行不能编辑
    if (indexPath.row == 5) {
        return NO;
    }
    if ([[list objectAtIndex:indexPath.row] isEqualToString:@"孙悟空"]) {
        return NO;
    }
    return YES;
}
//=============================
//实现移动的操作只需两步
//1、把编辑状态打开
//2、进行以下方法:-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

//=============================
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSInteger sourceRow = sourceIndexPath.row;
    NSInteger destRow = destinationIndexPath.row;
    
    id targetObj = [list objectAtIndex:sourceRow];
    [list removeObjectAtIndex:sourceRow];
    [list insertObject:targetObj atIndex:destRow];
}
//这是是工具条上的两个按钮所绑定的方法
- (IBAction)toggleEdit:(id)sender {
    if ([[sender title] isEqualToString:@"删除"]) {
        action = 0;
    }else
    {
        action = 1;
    }
    
    [self.table setEditing:!self.table.editing animated:YES];
    
    if (self.table.editing) {
        self.addbtn.title = @"完成";
        self.deletebtn.title = @"完成";
    }else
    {
        self.addbtn.title = @"增加";
        self.deletebtn.title = @"删除";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
编辑表格@tableView实现增加、删除、移动的操作_第1张图片

                                                  上方图片1

编辑表格@tableView实现增加、删除、移动的操作_第2张图片

你可能感兴趣的:(ios,UI,tableview,控件)