tableView编辑

#pragma mark TableView编辑


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

- (void)editButton:(id)sender

{

    // edit:添加事件

    [_table setEditing:YES animated: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) {

        return UITableViewCellEditingStyleDelete;

    }

    return UITableViewCellEditingStyleInsert;

}


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

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

{

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [_tableArray removeObjectAtIndex:indexPath.row];

//

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

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

//    [tableView reloadData];

        

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

        NSArray *array = @[indexPath];

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

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

        

        [tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];

    }else if (editingStyle == UITableViewCellEditingStyleInsert){

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

        [_tableArray addObject:s4];

//        [tableView reloadData];

        NSIndexPath *myIndex = [NSIndexPath indexPathForRow:[_tableArray count] - 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 = [[_tableArray objectAtIndex:sourceIndexPath.row] retain];

    

    [_tableArray removeObjectAtIndex:sourceIndexPath.row];

    

    [_tableArray insertObject:model atIndex:destinationIndexPath.row];

    

    [tableView reloadData];

    

    [model release];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值