0.2.2.tableView数据更新
- 重新刷新屏幕上的所有cell
[self.tableView reloadData];
- 刷新特定行的cell
[self.tableView reloadRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0] ] withRowAnimation:UITableViewRowAnimationLeft];
- 插入特定行数的cell
[self.tableView insertRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0] ] withRowAnimation:UITableViewRowAnimationLeft];
- 删除特定行数的cell
[self.tableView deleteRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0] ] withRowAnimation:UITableViewRowAnimationLeft];
- 编辑模式
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
/**
* 只要实现这个方法,左划cell出现删除按钮的功能就有了
* 用户提交了添加(点击了添加按钮)\删除(点击了删除按钮)操作时会调用
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) { // 点击了“删除”
// 删除模型
[self.deals removeObjectAtIndex:indexPath.row];
// 刷新表格
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
} else if (editingStyle == UITableViewCellEditingStyleInsert) { // 点击了+
NSLog(@"+++++ %zd", indexPath.row);
}
}
/**
* 这个方法决定了编辑模式时,每一行的编辑类型:insert(+按钮)、delete(-按钮)
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row % 2 == 0? UITableViewCellEditingStyleInsert: UITableViewCellEditingStyleDelete;
}
数据刷新的原则
- 通过修改模型数据,来修改tableView的展示
- 先修改模型数据
- 再调用数据刷新方法
- 不要直接修改cell上面子控件的属性
表格数据的批量操作
原则 记录改变都由模型来记录
多行删除
1.model添加两个属性
//在模型中添加这两个属性
//记录选择的model
@property (assign, nonatomic,getter=isSelectOK) BOOL selectOK;
//记录选择的行号
@property (nonatomic, strong) NSIndexPath *selectNum;
2.在cell的setMOdel方法中
self.selectView.image = deal.selectOK ? [UIImage imageNamed:@"selectBankOK1"]:[UIImage imageNamed:@"selectBank"];
3.ViewController做的操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//取消选中这一行
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//模型的打钩属性取反
XMGDeal *deal = self.deals[indexPath.row];
deal.selectOK = !deal.isSelectOK;
//记录选择的行号
deal.selectNum = indexPath;
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
#pragma mark - 按钮事件
- (IBAction)remove:(id)sender {
NSMutableArray<XMGDeal *> *deleteArr = [NSMutableArray array];
NSMutableArray<NSIndexPath *> *deleteNumArr = [NSMutableArray array];
for (XMGDeal *deal in self.deals) {
if (deal.selectOK) {
[deleteArr addObject:deal];
[deleteNumArr addObject:deal.selectNum];
}
}
[self.deals removeObjectsInArray:deleteArr];
[self.tableView deleteRowsAtIndexPaths:deleteNumArr withRowAnimation:UITableViewRowAnimationLeft];
}
系统自带的多行删除
- (void)viewDidLoad {
[super viewDidLoad];
// 允许在编辑模式进行多选操作
self.tableView.allowsMultipleSelectionDuringEditing = YES;
}
- (IBAction)remove:(id)sender {
// 获得所有被选中的行
NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
// 遍历所有的行号
NSMutableArray *deletedDeals = [NSMutableArray array];
for (NSIndexPath *path in indexPaths) {
[deletedDeals addObject:self.deals[path.row]];
}
// 删除模型数据
[self.deals removeObjectsInArray:deletedDeals];
// 刷新表格 一定要刷新数据
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];