iOS UItableView+UINavigationController实现单元格的删除

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    arrlist =[NSMutableArray arrayWithObjects:@"老王",@"小王",@"老李",@"小李",@"老张",@"小张",@"许仙",@"法海",@"孽畜",@"小刘",@"老刘",@"老孙",@"小孙",@"老陈", nil];

    


    

    UIView *VC=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    //VC.backgroundColor=[UIColor cyanColor];

    

    

    tableView=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];

    tableView.delegate=self;

     tableView.dataSource=self;

    

    //在导航栏添加右侧按钮

    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithTitle:@"移动" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleMove)];

    //添加到导航栏的右侧

    self.navigationItem.rightBarButtonItem=rightItem;


    

    

    [VC addSubview:tableView];

    self.view=VC;

    

    

// Do any additional setup after loading the view.

}


//动态的切换导航栏右侧按钮文字,并真正的更改单元格的状态

-(void)toggleMove{

//先获取当前表格的编辑状态

    [self.tableView setEditing:!self.tableView.editing animated:YES];

    

    

//判断,如果是编辑,文字要显示完成

    if (self.tableView.editing) {

//设置导航栏的右侧按钮的文字

        self.navigationItem.rightBarButtonItem.title=@"完成";

        

    }else{

//如果不是编辑状态,显示移动

        //设置导航栏的右侧按钮的文字

        self.navigationItem.rightBarButtonItem.title=@"移动";

    }

    

    

}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [arrlist count];

}






-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *simpleTable=@"SimpletableItem";

    UITableViewCell *cell=[self.tableView dequeueReusableCellWithIdentifier:simpleTable];

    if (cell==nil) {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTable];

        

    }

    cell.textLabel.text=[arrlist objectAtIndex:indexPath.row];

    

    return cell;

}


//提交删除的方法

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

    

    

    if (editingStyle==UITableViewCellEditingStyleDelete) {

        //进行删除

        [self.arrlist removeObjectAtIndex:indexPath.row];

        

        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        

    }else if (editingStyle==UITableViewCellEditingStyleInsert){

        //定义要插入的位置的数组

        NSArray *insertIndexPaths=[NSArray arrayWithObjects:indexPath, nil];

        //数据同步放到数组中

        [self.arrlist insertObject:@"insert new Cell" atIndex:indexPath.row];

        //添加显示

        [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];

      

    }

    

    

    

}





// 真是实现可以从fromIndexPath 移动到 toIndexPath;

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

{

    

    //获取当前行和要移动的行

    int fromRow = fromIndexPath.row;

    int toRow = toIndexPath.row;

    //保存将要移动的数据

    id fromObj = [self.listData objectAtIndex:fromRow];

    //删除原来的数据

    [self.listData removeObjectAtIndex:fromRow];

    [listData insertObject:fromObj atIndex:toRow];

    

//    for (NSString *name in listData) {

//        NSLog(@"name:%@",name);

//    }

    

}


//让表格可以移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}



- (UITableViewCellEditingStyle)tableView:

(UITableView *)tableView  editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {

return UITableViewCellEditingStyleInsert;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值