在UITableView中删除或添加行ui
- (void)tableView:commitEditingStyle:forRowAtIndexPath:spa
- (UITableViewCellEditingStyle)tableView: editingStyleForRowAtIndexPath:代理
1.1 加载:code
UITableViewCell 的加载须要遵照UITableViewDataSource数据源协议中的三个方法:blog
@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 滑动删除:rem
只实现系统自带的滑动删除功能 只须要实现数据源下面一个方法:get
// 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->添加中文支持it
1.3 编辑模式下的删除、添加io
若是不滑动,经过点击系统自带的添加和删除按钮进行添加、删除操做 那么得让表格的可编辑属性为YES才行,好比:点击一个按钮后让表格进入编辑模式:table
#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]; }