iOS TableView 自带可拖动重排功能

来源:互联网 发布:100m网络下载速度 编辑:程序博客网 时间:2024/05/01 07:18


1.首先设置可以编辑

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

2.对cell的编辑样式进行设置, 点击cell 右边的按钮可以 拖动进行重新排序

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

    return UITableViewCellEditingStyleInsert;

    

}


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

    return YES;

}


//对数据源进行重新排序

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

     [models exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

}

//如果是删除和添加的话,不过需要在style中返回要使用的cell的style

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

    if (editingStyle == UITableViewCellEditingStyleInsert) {

        //添加操作

        [models insertObject:@"xxgxgxu" atIndex:indexPath.row];

    }

    else if (editingStyle == UITableViewCellEditingStyleDelete) {

        //删除操作

        [models removeObjectAtIndex:indexPath.row];

    }

    [self.tableView reloadData];

}





//tabelview的界面 中cell的样式, 自带的有添加,删除,排序,
.若需要重写一种长按整个cell都可以进行重排, 需要参考别人写的.
/http://www.jianshu.com/p/638b22e40244

0 0