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

来源:互联网 发布:局域网网络管理 编辑:程序博客网 时间:2024/05/05 01:42

在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 0
原创粉丝点击