在UITableView中删除或添加行

 

•1. 开启表格的编辑模式
•2. 实现UITableViewDataSource的方法:

- (void)tableView:commitEditingStyle:forRowAtIndexPath:

•3. 实现UITableViewDelegate的方法:

- (UITableViewCellEditingStyle)tableView: editingStyleForRowAtIndexPath:

•注意:如果不实现该方法,默认将编辑模式视为删除

1.1 加载:

UITableViewCell 的加载需要遵守UITableViewDataSource数据源协议中的三个方法:

 

@protocol UITableViewDataSource<NSObject>
@required

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

@optional

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented

 

   1.2 滑动删除:

  只实现系统自带的滑动删除功能 只需要实现数据源下面一个方法:

// After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
// Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

 

/**
 *  如果实现了这个方法,就自动实现了滑动删除的功能
 *  点击了删除按钮就会调用
 *  提交了一个编辑操作就会调用(操作:删除\添加)
 *  @param editingStyle 编辑的行为
 *  @param indexPath    操作的行号
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(editingStyle == UITableViewCellEditingStyleDelete){
        [self.contactList removeObjectAtIndex:indexPath.row];
        
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        
        // 更新数据至文件
        BOOL isSuccess = [NSKeyedArchiver archiveRootObject:self.contactList toFile:self.docPath];
        NSLog(@"%@",isSuccess?@"保存成功":@"保存失败");
    }else if (editingStyle == UITableViewCellEditingStyleInsert){
        // 添加行 一般很少使用这种方式
        HJContact *contact = [[HJContact alloc] init];
        contact.name = @"电信";
        contact.tel = @"10000";
        [self.contactList insertObject:contact atIndex:indexPath.row+1];
        
        // 刷新表格
        NSIndexPath *indexPathInsert = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[indexPathInsert] withRowAnimation:UITableViewRowAnimationTop];
    }
}

 这样就可以实现cell滑动删除,默认删除按钮是英文,点击项目名->project->Localizations->添加中文支持

  1.3 编辑模式下的删除、添加

  如果不滑动,通过点击系统自带的添加和删除按钮进行添加、删除操作 那么得让表格的可编辑属性为YES才行,比如:点击一个按钮后让表格进入编辑模式:

#warning 导航栏删除按钮方法
- (void)cellDelete{
    NSLog(@"%s",__func__);
//    self.tableView.editing = !self.tableView.editing;
    [self.tableView setEditing:!self.tableView.editing animated:YES];
}

此时cell会进行编辑模式,接着会调用下面方法

#pragma mark tableView代理方法
/**
 *  当tableView进入编辑状态的时候会调用,询问每一行进行怎样的操作(添加\删除)
 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row %2 ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}

 注意:这个两个方法,一个只是让表格可编辑,接着调用

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
方法,告诉tableView那些行是删除或添加,然后再接着调用
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

 真正的添加、删除操作都在此方法中进行.

 

#pragma mark 表格控件提交编辑模式
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"编辑样式 %d", editingStyle);
    
    // 判断表格是否需要删除数据
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 1. 删除_dataList中的数据
        [_dataList removeObjectAtIndex:indexPath.row];
        NSLog(@"%@", _dataList);
        
        // 2. 更新表格显示
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    } else {
        // 1. 在_dataList中插入数据
        //      可以在传入参数indexPath.row + 1 的位置插入数据
        //      也就是用户当前选中行的下一行
        [_dataList insertObject:@"新增数据" atIndex:(indexPath.row + 1)
         ];
        NSLog(@"%@", _dataList);
        
        // 2. 更新表格显示
        // 界面上添加数据的newPath.row应该是参数indexPath.row + 1
        NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0];
        [_tableView insertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}

#pragma mark 返回表格的编辑样式:删除、新增 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { // NSLog(@"告诉表格编辑样式"); return _tableView.tag; } #pragma mark - Actions #pragma mark 删除操作 - (IBAction)remove:(id)sender { [_tableView setTag:UITableViewCellEditingStyleDelete]; // 再次点击删除按钮,要恢复到正常浏览模式 // 编辑|浏览 // 根据表格的isEditing属性可以判断出当前是否处于编辑状态 // 如果是编辑状态,则改为浏览状态,否则,改为编辑状态 BOOL isEditing = _tableView.isEditing; // 1. 开启表格的编辑模式 [_tableView setEditing:!isEditing animated:YES]; } #pragma mark 添加操作 - (IBAction)add:(id)sender { /** 新增或者删除操作方法的执行顺序 1. 由按钮开启编辑样式 setEditing 2. 表格会对屏幕上每一个行调用 tableView:editingStyleForRowAtIndexPath: 告诉当前的行是什么编辑样式(新增或是删除) 注意:如果不实现这一方法,默认都是删除样式 3. 当用户点击屏幕上相关按钮:加好或者减号,会调用: tableView:commitEditingStyle:forRowAtIndexPath: 方法,控制器需要在此方法中更新数据,同时更新界面 */ // 利用表格的tag属性记录当前的编辑样式是新增 [_tableView setTag:UITableViewCellEditingStyleInsert]; // 判断表格当前是否是编辑模式,如果是,进入浏览状态,如果不是,进入编辑状态 BOOL isEditing = _tableView.isEditing; NSLog(@"开启编辑模式"); // 开启表格的编辑模式 [_tableView setEditing:!isEditing animated:YES]; }

 

 

 

 

 

posted on 2014-12-31 23:58  HJiang  阅读(2276)  评论(0编辑  收藏  举报