iOS边练边学--简单的数据操作(增、删、改),左滑动删除和弹窗
2016-03-18 14:46
666 查看
一、数据刷新的原则:
通过修改模型数据,来修改tableView的展示
先修改数据模型
在调用数据刷新方法
不要直接修改cell上面子控件的属性
二、增删改用到的方法:
<1>重新绑定屏幕上所有的cell,这个方法没有动画效果,但是以下三种方法通过这个方法都可以办到
<2>刷新特定的cell,可以设置动画效果
<3>插入特定行数的cell,可以设置动画效果
<4>删除特定行数的cell,可以设置动画效果
三、
<1>左滑动删除效果,需要实现tableView的代理方法。实现该方法后默认实现的是左滑动有删除按钮,但是这个方法会处理两种情况:删除和添加
![](http://images2015.cnblogs.com/blog/762322/201603/762322-20160318142015553-222513971.png)
<2>配合下面这个方法,可以决定编辑的类型,前提是self.tableView.editing = YES;
![](http://images2015.cnblogs.com/blog/762322/201603/762322-20160318142653631-1256600510.png)
四、创建弹框控制器iOS8增加的新功能,其中屏幕下方的弹窗中不能添加文本框,否则会报错
![](http://images2015.cnblogs.com/blog/762322/201603/762322-20160318144318881-853466309.png)
通过修改模型数据,来修改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的代理方法。实现该方法后默认实现的是左滑动有删除按钮,但是这个方法会处理两种情况:删除和添加
#pragma mark - tableView代理方法 // 只要实现了这个方法,左滑cell就会出现删除按钮 // 调用时机:用户提交了添加(点击了添加按钮)\删除(点击了删除按钮)操作时会调用 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleInsert) { // 添加 } else { // 删除 [self.dealArray removeObjectAtIndex:indexPath.row]; // [self.tableView reloadData]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } }
![](http://images2015.cnblogs.com/blog/762322/201603/762322-20160318142015553-222513971.png)
<2>配合下面这个方法,可以决定编辑的类型,前提是self.tableView.editing = YES;
// 这个方法决定了编辑模式时,每一行的编辑类型:inset(+按钮) delete(—按钮) 不实现这个方法默认返回的是delete - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return indexPath.row % 2 ? UITableViewCellEditingStyleInsert:UITableViewCellEditingStyleDelete; }
![](http://images2015.cnblogs.com/blog/762322/201603/762322-20160318142653631-1256600510.png)
四、创建弹框控制器iOS8增加的新功能,其中屏幕下方的弹窗中不能添加文本框,否则会报错
// 创建弹框控制器,弹框有两种,一种是在中间的一种,另一种是从最下面往上展现的一种 // UIAlertControllerStyleAlert(中间的弹窗) // UIAlertControllerStyleActionSheet(底端的弹窗) UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入团购信息" message:nil preferredStyle:UIAlertControllerStyleAlert]; // 添加按钮 最后的block参数是点击按钮后执行的代码,取消按钮中的block可以不用设置 [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; // 在点击确定按钮的block中做相应的操作 [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { // 创建模型 XMGDeal *deal = [[XMGDeal alloc] init]; // 可以通过alert弹窗中的textFields集合属性获得弹窗中对应文本框的值 deal.title = [alert.textFields[0] text]; deal.price = [alert.textFields[1] text]; [self.deals insertObject:deal atIndex:0]; // 刷新数据 [self.tableView reloadData]; }]]; // 添加文本输入框 [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { // 设置占位字符,对于用户有一定的提示作用 textField.placeholder = @"请输入团购名字"; }]; [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"请输入团购价格"; }]; // 将弹窗展现出来动画效果,显示控制器 [self presentViewController:alert animated:YES completion:nil];
![](http://images2015.cnblogs.com/blog/762322/201603/762322-20160318144318881-853466309.png)
![](http://images2015.cnblogs.com/blog/762322/201603/762322-20160318144343318-515517878.png)
相关文章推荐
- iOS-代码修改Info.plist文件
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解[转]
- nagios-安装配置
- IOSGCD
- ios: Base SDK和Deployment Target
- ios 模态半透明弹出框
- iOS 证书
- 【整理】BIOS、BootLoader、uboot对比
- ios 字符串转date,模拟器ok,真机不ok,
- iOS核心动画高级技巧一
- iOS计时器NSTimer的使用
- nagios监控系统安装
- iOS-----About Asset Catalog
- nagios短信监控
- ios开发中,联系人列表是怎样通过名字排序分组的(支持中文排序)
- IOS项目自动生成技术文档很方便实用
- iOS模拟Tom猫的简单界面
- iOS下bound,center和frame
- iOS cocoapods的详解大全及其flex
- iOS8之后CoreLocation定位的使用