在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.dea