UI - UITableView 移动

来源:互联网 发布:东莞网络外包公司 编辑:程序博客网 时间:2024/06/03 19:28
//在TableView编辑 篇章中加入/*************************** TableView 的移动 ***************************/#pragma mark--移动第一步: 让tableView处于编辑状态//Table的协议方法- (void)setEditing:(BOOL)editing animated:(BOOL)animated{    //1.    [super setEditing:editing animated:animated];    //2.    [self.tableView setEditing:editing animated:animated];}#pragma mark--移动第二步: 指定哪行可以移动- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}#pragma mark--移动限制: 限制跨区移动- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{//    sourceIndexPath: 移动之前的位置//    proposedDestinationIndexPath: 想去的位置(松手的位置)    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {        return proposedDestinationIndexPath;    }    return sourceIndexPath;}#pragma mark--移动第三步: 移动完成- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    //移动不需要删除cell(UI)    //1.删除数据源    NSMutableArray *arr1 = [self.dataArr objectAtIndex:sourceIndexPath.section];    NSString *str = [arr1 objectAtIndex:sourceIndexPath.row];    [arr1 removeObjectAtIndex:sourceIndexPath.row];    //2.添加数据 cell移动之后所在的分组    NSMutableArray *arr2 = [self.dataArr objectAtIndex:destinationIndexPath.section];    [arr2 insertObject:str atIndex:destinationIndexPath.row];}
0 0