在tableView中进行数据的添加删除更新操作,其实就是对模型数据就行修改,然后再让tableView重新load一遍数据的过程,主要是一些api的认识。
1、添加行:
- (IBAction)add {
XXModel *model = [[XXModel alloc]init];
//设置model的属性数据
//...
//添加在最前面的一行
[self.models insertObject:model atIndex:0];
//通知tableView数据源发生了改变,需要重新加载数据
//[self.tableView reloadData];
//少行操作时,insertRowsAtIndexPaths效率更高
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
//也可以一次添加多行,但是添加的对象个数一定要和NSIndexPath数组对应
[self.tableView insertRowsAtIndexPaths:@[
[NSIndexPath indexPathForRow:0 inSection:0]
[NSIndexPath indexPathForRow:1 inSection:0]
] withRowAnimation:UITableViewRowAnimationTop];
}
2、删除行:
- (IBAction)romove {
[self.deals removeObjectAtIndex:0];
// [self.tableView reloadData];
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}
3、更新某行数据:
- (IBAction)update {
XXModel *model = self.models[3];
//修改model数据
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
类似微信那种在tableView中某行左滑显示删除按钮的功能api:
#pragma mark -tableView代理方法
/**
* 只要实现这个方法,左划cell出现删除按钮的功能就有了
* 用户提交了添加(点击了添加按钮)\删除(点击了删除按钮)操作时会调用
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//editingStyle的值默认是UITableViewCellEditingStyleDelete
if (editingStyle == UITableViewCellEditingStyleInsert) {
//添加操作
}else if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除操作
}
}
另外还有tableView的编辑模式,设置编辑模式:
// 进入编辑模式
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
// 允许在编辑模式进行多选操作,用作批量操作
self.tableView.allowsMultipleSelectionDuringEditing = YES;
/**
* 这个方法决定了编辑模式时,每一行的编辑类型:insert(+按钮)、delete(-按钮)
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return indexPath.row % 2 == 0 ? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete;
}
如果要想左滑弹出多个按钮,在iOS8.0之后可以通过这个方法实现:
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"置顶");
}];
action.backgroundColor = [UIColor orangeColor];
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"订阅" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"订阅");
}];
action1.backgroundColor = [UIColor grayColor];
UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"删除");
}];
return @[action,action1,action2];
}
效果图: