UIKit框架-12.UITableView数据刷新
2015-07-29 16:20
495 查看
1.全局数据刷新
// 添加三个按钮实现TableView的增删改操作 // 注意: // 1.我们在修改UITableView的数据时,都是通过操作模型来修改数据的,而不会直接拿出cell的数据修改 // 2.因为TableView是根据模型数组来加载每一行的数据的,我们修改了cell的数据并没有修改对模型数组的数据,当cell重新加载时又会重新加载模型数组的内容,所以不能通过此方法修改cell #pragma mark - 数据刷新操作 // 添加数据操作 - (IBAction)add { // 添加模型数据 ZJMode *model = [[ZJModle alloc] init]; // 修改模型中的属性 model.money = @"20"; model.name = @"新商品"; model.image = @"new_image"; // 添加到数组 [self.modelArray insertObject:wine atIndex:0]; // [self.modelArray addObject:model];会将数据添加到最后 // reloadData方法:会刷新整个TableView [self.tableView reloadData]; } // 删除数据操作 - (IBAction)remove { // 删除模型数据 [self.modelArray removeObjectAtIndex:0]; [self.modelArray removeObjectAtIndex:0]; // 刷新表格 [self.tableView reloadData]; }
2.局部数据刷新
局部刷新操作RowsAtIndexPaths:传入要刷新的位置(数组(存放NSIndexPath对象))
withRowAnimation:刷新操作的动画效果
- (IBAction)add:(id)sender { // 添加一行数据 ZJWine *wine = [[ZJWine alloc] init]; wine.image = @"new_wine.png"; wine.money = @"99.8"; wine.name = @"全球首发,晶莹剔透,口感酸爽"; [self.wines insertObject:wine atIndex:0]; // 插入刷新数据 [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic]; } - (IBAction)delete:(id)sender { // 移除数组中的某些数据 [self.wines removeObjectAtIndex:0]; // 局部删除 [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft]; } - (IBAction)change:(id)sender { //修改数组中的模型数据 ZJWine *wine = self.wines[0]; wine.name = @"修改酒名1"; wine.money = @"998"; // 刷新tableview(局部刷新) [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft]; }
3.左滑出现删除按钮
- 需要实现tableView的代理方法
/** * 只要实现了这个方法,左滑出现Delete按钮的功能就有了 * 点击了“左滑出现的Delete按钮”会调用这个方法 */ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // 点击删除按钮删除数据 // 删除模型 [self.wineArray removeObjectAtIndex:indexPath.row]; // 刷新 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; }
修改Delete按钮文字为“删除”,实现代理方法
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"删除"; }
4.左滑出现N个按钮
- 需要实现tableView的代理方法
/** * 只要实现了这个方法,左滑出现按钮的功能就有了 (一旦左滑出现了N个按钮,tableView就进入了编辑模式, tableView.editing = YES) */ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { }
左滑cell时出现什么按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { // 左滑出现的按钮类型是UITableViewRowAction // rowActionWithStyle:传入枚举 /*typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) { UITableViewRowActionStyleDefault = 0, 默认样式 UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault, 摧毁样式即红色加粗样式 UITableViewRowActionStyleNormal 正常样式 } NS_ENUM_AVAILABLE_IOS(8_0); */ // title:按钮文字 // handler:点击按钮执行的代码块 UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"点击了关注"); // 收回左滑出现的按钮(退出编辑模式) tableView.editing = NO; }]; UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { [self.wineArray removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }]; return @[action1, action0]; }
5.编辑模式
进入编辑模式// self.tabelView.editing = YES; [self.tableView setEditing:YES animated:YES]; // 默认情况下,进入编辑模式时,左边会出现一排红色的“减号”按钮
- 在编辑模式中多选
// 编辑模式的时候可以多选 self.tableView.allowsMultipleSelectionDuringEditing = YES; // 进入编辑模式 [self.tableView setEditing:YES animated:YES]; // 获得选中的所有行 self.tableView.indexPathsForSelectedRows;
相关文章推荐
- Ubiquitous Religions(POJ--2524
- Unique Path II
- IOS开发概述-09.UIController和控制器的View
- 【进阶android】Volley源码分析——Volley的工具【StringRequest】
- iOS开发使用Unwind Segue进行返回
- Spark SQL and DataFrame Guide(1.4.1)——之DataFrames
- UIActionSheet
- 云盘+Git GUI云盘文件版本控制
- ios开发之实现长按UITableViewCell弹出UIMenuController
- Android Build属性介绍
- apue 第十一章 线程
- C#中Queue<T>类的使用以及部分方法的源码分析
- C#中Queue<T>类的使用以及部分方法的源码分析
- muduo::BlockingQueue、BoundedBlockingQueue分析
- SPOJ 1771 NQUEEN Yet Another N-Queen Problem
- 通用Key-Value存储系统的存储管理策略解析
- Architecture,Valid architectures,Build Active Architecture Only
- 配置文件读取
- UISearchBar 点击取消回到原来位置时会跳动的解决方法
- Maximum Subarray