(一二四)tableView的多组数据展示和手动排序
来源:互联网 发布:浪潮java面试题 编辑:程序博客网 时间:2024/06/04 01:02
最近在写一个轻量级的网络游戏,遇到了技能优先顺序手动排序的需求,我就想到了iOS自带的tableView编辑功能,对其进行了初步探索,最后做出的效果如下图所示:
点击左边可以删除,拖住右边可以手动排序,要实现这个功能,分以下步骤。
①用plist存储这些数据,可以看到数据分两个职业,每个职业4个技能,因此建立如下的plist结构:
②因为每个职业除了技能还有名称这个属性,因此应该用职业模型保存一个职业的所有数据,再用一个数组保存所有职业模型,职业模型的定义如下:
#import <Foundation/Foundation.h>@interface Vocation : NSObject@property (nonatomic, strong) NSString *title;@property (nonatomic, strong) NSMutableArray *skills;+ (instancetype)vocationWithDict:(NSDictionary *)dict;@end需要注意的是这里没有利用系统实现KVC,因为如果采用系统自带的,在把plist中的NSArray传给NSMutableArray时,因为NSMutableArray没有初始化 ,所以就变成了不可变的数组,这样为后面的顺序调整带来了致命的问题,因此我们手动实现KVC,用NSArray初始化一个NSMutableArray。
#import "Vocation.h"@implementation Vocation+ (instancetype)vocationWithDict:(NSDictionary *)dict{ Vocation *vc = [[Vocation alloc] init]; vc.title = dict[@"title"]; vc.skills = [NSMutableArray arrayWithArray:dict[@"skills"]]; return vc; }@end③使用一个TableViewController,并且实现下面的代码:
#import "TableViewController.h"#import "Vocation.h"@interface TableViewController ()@property (nonatomic, strong) NSArray *vocations;@end@implementation TableViewController- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; self.editing = YES; }- (BOOL)prefersStatusBarHidden{ return YES; }- (NSArray *)vocations{ if (_vocations == nil) { NSString *path = [[NSBundle mainBundle] pathForResource:@"skillList.plist" ofType:nil]; NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; NSMutableArray *vocations = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { Vocation *vc = [Vocation vocationWithDict:dict]; [vocations addObject:vc]; } _vocations = vocations; } return _vocations; }- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.vocations.count;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ Vocation *vc = self.vocations[section]; return vc.skills.count; }- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ Vocation *vc = self.vocations[section]; return vc.title; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"vocation"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } // 在这里设置cell数据 Vocation *vc = self.vocations[indexPath.section]; cell.textLabel.text = vc.skills[indexPath.row]; return cell; }- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ if (sourceIndexPath.section != destinationIndexPath.section) { [self.tableView reloadData]; return; } Vocation *vc = self.vocations[sourceIndexPath.section]; [vc.skills exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; }- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { Vocation *vc = self.vocations[indexPath.section]; [vc.skills removeObjectAtIndex:indexPath.row]; } [self.tableView reloadData]; }- (IBAction)editClick:(id)sender { UIBarButtonItem *btn = sender; if ([btn.title isEqualToString:@"调整"]) { btn.title = @"确定"; self.editing = YES; }else{ btn.title = @"调整"; self.editing = NO; [self.tableView reloadData]; } }@end在这其中,editClick:对应了NavigationBar上的按钮,用于切换编辑和非编辑状态。
通过tableViewController的editing方法控制是否进入编辑状态。
要实现拖动排序,需要实现下面的方法,否则不能拖动,在这个方法中可以获取到起始和终止位置。
需要注意的是移动只是单纯的视觉效果,实际的数据源变化需要自己调整,否则在重新加载数据后又会回到原来的顺序,可通过数组的exchangeObjectAtIndexPath::方法调整。
为了避免组间移动,这里进行了判断,发现非法移动直接重置数据。
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ if (sourceIndexPath.section != destinationIndexPath.section) { [self.tableView reloadData]; return; } Vocation *vc = self.vocations[sourceIndexPath.section]; [vc.skills exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; }
0 0
- (一二四)tableView的多组数据展示和手动排序
- tableView展示数据的过程
- 封装展示没有数据的tableview
- swift+tableview+数据展示
- (十八)TableView实践(多组汽车品牌展示)
- 基于DataList的数据分页和排序展示
- tableView自定义cell与api数据展示纪录(也是MVC的例子)
- 在tableView上展示数据时遇到的问题
- 【iOS代码】Json数据展示为联系人效果的TableView
- tableView 使用注意事项:一个tableView展示不同的cell时复用,以及数据更新
- 基于省市区展示的tableView多层展开和折叠
- Swift练习一: 使用TableView展示数据
- 06-UI-TableView展示数据三部曲”
- ios tableView那些事(四)tableView添加标题,头尾和改变cell的宽度---tableview
- 图表展示说明一二
- iphone开发之表格组件UITableView的使用(四) 通过加载plist文件展示单组数据
- 项目管理的一二四法则
- 用百度map api展示时空数据(四)--ajax的实现
- ssh 反向隧道
- 图结构练习——BFSDFS——判断可达性--------BFS——从起始点到目标点的最短步数
- OC类的深入研究
- 【Maven】跳过test测试直接进行编译打包
- SVM 多分类 -SVM分类opencv3.0源代码
- (一二四)tableView的多组数据展示和手动排序
- BZOJ 1695 [Usaco2007 Demo]Walk the Talk 链表+数学
- python笔记:深刻理解Python中的元类(metaclass)
- poj-3126-Prime Path-BFS
- git 提交前撤销add操作
- tools:context=".MainActivity的作用
- js弹出框、对话框、提示框、弹窗总结
- poj-3126 Prime Path
- android通知 notification