打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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;  // 这样左边不会出现删除操作时出现的红色按钮

}



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
讲解iOS开发中UITableView列表设计的基本要点
UITableView 实现Cell删除操作
ios UITableView封装之下拉
iOS开发 精简TableView
UITableView优化技巧 | iOS开发
UITableView侧滑删除
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服