iOS边练边学--简单的数据操作(增、删、改),左滑动删除和弹窗

一、数据刷新的原则:

  • 通过修改模型数据,来修改tableView的展示
    • 先修改数据模型
    • 在调用数据刷新方法
  • 不要直接修改cell上面子控件的属性

二、增删改用到的方法:

  <1>重新绑定屏幕上所有的cell,这个方法没有动画效果,但是以下三种方法通过这个方法都可以办到

    // 重新加载数据,刷新的是整个页面,没有动画
    [self.tableView reloadData];

  <2>刷新特定的cell,可以设置动画效果

    // 刷新指定的cell
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];

  <3>插入特定行数的cell,可以设置动画效果

     // 只是刷新添加的数据,可以同时设置动画
    [self.tableView insertRowsAtIndexPaths:@[
                                             [NSIndexPath indexPathForRow:0 inSection:0],
                                             [NSIndexPath indexPathForRow:1 inSection:0]
                                             ] withRowAnimation:UITableViewRowAnimationMiddle];

  <4>删除特定行数的cell,可以设置动画效果

    [self.dealArray removeObjectAtIndex:0];
    [self.tableView deleteRowsAtIndexPaths:@[
                                             [NSIndexPath indexPathForRow:0 inSection:0]
                                             ] withRowAnimation:UITableViewRowAnimationFade];

三、

  <1>左滑动删除效果,需要实现tableView的代理方法。实现该方法后默认实现的是左滑动有删除按钮,但是这个方法会处理两种情况:删除和添加

 1 #pragma mark - tableView代理方法
 2 // 只要实现了这个方法,左滑cell就会出现删除按钮
 3 // 调用时机:用户提交了添加(点击了添加按钮)删除(点击了删除按钮)操作时会调用
 4 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 5 {
 6     if (editingStyle == UITableViewCellEditingStyleInsert) { // 添加
 7         
 8     } else { // 删除
 9         
10         [self.dealArray removeObjectAtIndex:indexPath.row];
11         //    [self.tableView reloadData];
12         [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
13     }
14 }

  <2>配合下面这个方法,可以决定编辑的类型,前提是self.tableView.editing = YES;

1 // 这个方法决定了编辑模式时,每一行的编辑类型:inset(+按钮) delete(—按钮)  不实现这个方法默认返回的是delete
2 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
3 {
4     return indexPath.row % 2 ? UITableViewCellEditingStyleInsert:UITableViewCellEditingStyleDelete;
5 }

四、创建弹框控制器iOS8增加的新功能,其中屏幕下方的弹窗中不能添加文本框,否则会报错

 1 // 创建弹框控制器,弹框有两种,一种是在中间的一种,另一种是从最下面往上展现的一种
 2     // UIAlertControllerStyleAlert(中间的弹窗)
 3     // UIAlertControllerStyleActionSheet(底端的弹窗)
 4     UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入团购信息" message:nil preferredStyle:UIAlertControllerStyleAlert];
 5     
 6     // 添加按钮 最后的block参数是点击按钮后执行的代码,取消按钮中的block可以不用设置
 7     [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
 8     // 在点击确定按钮的block中做相应的操作
 9     [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
10         // 创建模型
11         XMGDeal *deal = [[XMGDeal alloc] init];
12         // 可以通过alert弹窗中的textFields集合属性获得弹窗中对应文本框的值
13         deal.title = [alert.textFields[0] text];
14         deal.price = [alert.textFields[1] text];
15         [self.deals insertObject:deal atIndex:0];
16         
17         // 刷新数据
18         [self.tableView reloadData];
19     }]];
20     
21     // 添加文本输入框
22     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
23         // 设置占位字符,对于用户有一定的提示作用
24         textField.placeholder = @"请输入团购名字";
25     }];
26     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
27         textField.placeholder = @"请输入团购价格";
28     }];
29     
30     // 将弹窗展现出来动画效果,显示控制器
31     [self presentViewController:alert animated:YES completion:nil];

    

原文地址:https://www.cnblogs.com/gchlcc/p/5292063.html