UItableView编辑的步骤

self.navigationItem.rightBarButtonItem = self.editButtonItem;

第一步设置可编辑条件

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{    // 调用父类的(使其可以切换)
    [super setEditing:editing animated:YES];
    // 获取表表视图的类型(定义成实例变量)
    UITableView *tableView = (UITableView *)self.view;
    // 设置可编辑状态
    [tableView setEditing:editing animated:animated];
}

第二部设置进入编辑状态

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

第三部设置哪一个可以编辑

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return UITableViewCellEditingStyleDelete;
    }if (indexPath.section == 1) {
        return UITableViewCellEditingStyleInsert;
    }else
        return UITableViewCellEditingStyleDelete;
}

第四部设置可编辑状态的样式

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

第五部删除按钮标签名

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 删除数据
        // 拿到数据对象
        NSString *key = [self.groupNames objectAtIndex:indexPath.section];
        NSMutableArray *arry = [self.personDic objectForKey:key];
        
        // 把删除,插入都写到begin和end中
        [tableView beginUpdates];
        if ([arry count] > 1 ) {
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
            [arry removeObjectAtIndex:indexPath.row];
        }else
        {
            // 删除字典中的数据
            [self.personDic removeObjectForKey:key];
            // 删分区时分区数量也得相应的减一
            [self.groupNames removeObjectAtIndex:indexPath.section];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];
        }
        [tableView endUpdates];
        
    
    }else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        
    // 添加到指定位置
    // NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
        Person *p = [Person personWithName:@"张三" sex:@"男" phoneNumber:@"32321323" address:@"大学西路" email:@"43454656@qq.com" age:@"32"];
        // 通过key找出对应的数组信息
        NSString *key = [self.groupNames objectAtIndex:indexPath.section];
        // 通过键值找到对应的对象
        NSMutableArray *arry = [self.personDic objectForKey:key];
        // 在对应的数组中添加需要填加的对象
        [arry insertObject:p atIndex:indexPath.row];
        // 添加到当前行
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
    }
}
第六步进行具体的编辑操作




下面介绍一下如何移动数据(数据存储是通过字典的方式存储的)步骤如下

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSLog(@"%@", sourceIndexPath);

    // 获取移动数据对象
    NSString *sourceKey = [self.groupNames objectAtIndex:sourceIndexPath.section];
    NSMutableArray *sourceGrop = [self.personDic objectForKey:sourceKey];
    // 拿到数据对象
    Person *p = [sourceGrop objectAtIndex:sourceIndexPath.row];
    // 计数器加一次
    [p retain];
    // 拿到移动位置的数据位置
    NSString *destinationkey = [self.groupNames objectAtIndex:destinationIndexPath.section];
    // 拿到移动数据所在的数组
    NSMutableArray *destinationkeyGrop = [self.personDic objectForKey:destinationkey];
    // 移除原先的数据
    [sourceGrop removeObject:p];
    // 插入移动的后所在位置的数据
    [destinationkeyGrop insertObject:p atIndex:destinationIndexPath.row];
    // 释放数据
    [p release];
}
// 分区移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    // 若果时同一个分区则交换
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }else
    // 不在同一个分区则不移动
       return sourceIndexPath;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值