ios tableView那些事 (八) tableview的插入移动

在ios基础教程已经出现了这个经典的例子了!我也不费话了发代码!


 if (cell == nil)

    {

        

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

        cell.showsReorderControl =YES;  //我们添加一个重新排序控件

    }

    

在导航栏上加个 UIBarButtonItem;


 UIBarButtonItem *moveButton = [[UIBarButtonItemalloc]

                                   initWithTitle:@"移动"

                                   style:UIBarButtonItemStyleBordered

                                   target:self

                                   action:@selector(toggleMove)];

    self.navigationItem.rightBarButtonItem = moveButton;




事件响应

-(void)toggleMove

{

    

    [self.tableviewsetEditing:!self.tableview.editinganimated:YES];  //初始话时默认不可编辑 点击事件的时候取反 为真!可编辑

    

    if (self.tableview.editing)

    {

        [self.navigationItem.rightBarButtonItemsetTitle:@"完成"];

    }else

    {

        [self.navigationItem.rightBarButtonItemsetTitle:@"移动"]; 

    }

    

}

- (BOOL)tableView:(UITableView *)tableView

canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

     return YES; //可以移动

}

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

    NSUInteger fromRow = [fromIndexPath row];  //要移动的那个cell integer

    NSUInteger toRow = [toIndexPath row]; //要移动位置的那个clell integer

    //arrayValue 添加数据的那个可变数组

    id object = [arrayValueobjectAtIndex:fromRow]; // 获取数据

    [arrayValueremoveObjectAtIndex:fromRow];  //在当前位置删除

    [arrayValue insertObject:objectatIndex:toRow]; //插入的位置

}

//typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {

//    UITableViewCellEditingStyleNone,

//    UITableViewCellEditingStyleDelete,

//    UITableViewCellEditingStyleInsert


-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    returnUITableViewCellEditingStyleNone;  // 这样左边不会出现删除操作时出现的红色按钮

}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值