TableView编辑

来源:互联网 发布:彭博数据终端 编辑:程序博客网 时间:2024/05/29 07:36

#pragma mark TableView编辑


#warning 第一步:让tableView处于编辑状态(Warning警告的意思)

- (void)editButton:(id)sender

{

    // edit:添加事件

    [_tablesetEditing:YESanimated:YES];

}


#warning 第二步:通知tableview哪些cell可以被编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

//    if (indexPath.row == 0) {

//        return YES;

//    }

//    return NO;

    return YES;

}


#warning 第三步:cell的编辑模式

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

{

   if (indexPath.row ==0) {

        returnUITableViewCellEditingStyleDelete;

    }

    returnUITableViewCellEditingStyleInsert;

}


#warning 第四步:提交编辑结果

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

{

    //如果做的是删除操作,就把相对应的数组的那一行给删除了

    if (editingStyle ==UITableViewCellEditingStyleDelete) {

        [_tableArrayremoveObjectAtIndex:indexPath.row];

//

//    // 数据源(数组)发生改变,则必须让tableView的所有协议重新再走一遍,告诉tableView关于行数、内容的改变是什么

//    // 刷新tableView (最经常用的刷新方式)

//    [tableView reloadData];

        

        // 删除单个或多个行      数组中存得就是索要删除的IndexPath

       NSArray *array = @[indexPath];

//        // 如果没有Index的话,就自己创建(自己创建IndexPath的方法)

//        NSIndexPath *myIndex = [NSIndexPath indexPathForRow:0 inSection:0];

        

        [tableView deleteRowsAtIndexPaths:arraywithRowAnimation:UITableViewRowAnimationFade];

    }elseif (editingStyle ==UITableViewCellEditingStyleInsert){

        StudentModel *s4 = [StudentModelstudentModerWithName:@"老王"gender:@""teleNumber:@"15840959355"];

        [_tableArrayaddObject:s4];

//        [tableView reloadData];

       NSIndexPath *myIndex = [NSIndexPathindexPathForRow:[_tableArraycount] - 1 inSection:0];

        [tableView insertRowsAtIndexPaths:@[myIndex]withRowAnimation:UITableViewRowAnimationFade];

    }

    

}

#pragma mark - 

#pragma mark 移动


// 是否可以移动

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

{

//    [tableView reloadData];

    return YES;

}

// 移动sourceIndexPath:将要移动的 destinationIndexPath:目标

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

{

    //先改变数据源的顺序(从数组中拿出来的时候release,所以要先retain)

   StudentModel *model = [[_tableArrayobjectAtIndex:sourceIndexPath.row]retain];

    

    [_tableArrayremoveObjectAtIndex:sourceIndexPath.row];

    

    [_tableArrayinsertObject:model atIndex:destinationIndexPath.row];

    

    [tableViewreloadData];

    

    [modelrelease];

}


0 0
原创粉丝点击