iOS开发——开发必备OC篇&UITableView设置界面完整封装(四)
设置界面完整封装(四)
简单MVC实现UITableView设置界面完善封装及拓展使用
关于使用和拓展,
其实基本上就是同UItableView,知识讲数据改一下就可以
拓展使用
1:首先定义一个数组用来装组的模型
// 总共的组数 @property (nonatomic, strong) NSMutableArray *groups;
2:懒数组
- (NSMutableArray *)groups { if (_groups == nil) { _groups = [NSMutableArray array]; } return _groups; }
3:调用添加组的方法
- (void)viewDidLoad { [super viewDidLoad]; // 添加第0组 [self setUpGroup0]; }
4:设置组类型
- (instancetype)init { return [self initWithStyle:UITableViewStyleGrouped]; }
5:添加对应的行活着组类型
// 添加第0组 - (void)setUpGroup0 { // 创建行模型 // 开奖推送 iCocosSettingArrowItem *item = [iCocosSettingArrowItem itemWithImage:nil title:@"开奖推送"]; item.descVc = [UIViewController class]; // 比分直播 iCocosSettingArrowItem *item1 = [iCocosSettingArrowItem itemWithImage:nil title:@"比分直播"]; // 中奖动画 iCocosSettingArrowItem *item2 = [iCocosSettingArrowItem itemWithImage:nil title:@"中奖动画"]; // 购彩提醒 iCocosSettingArrowItem *item3 = [iCocosSettingArrowItem itemWithImage:nil title:@"购彩提醒"]; // Items:存储当前数组有多少行模型 // 创建一个组模型,描述第0组 iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[item,item1,item2,item3]]; // 添加组模型到groups数组,有多少个组模型就有多少组 [self.groups addObject:group]; }
6:实现数据元和代理方法
#pragma mark - 数据源 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.groups.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 取出当前的组模型 iCocosGroupItem * group = self.groups[section]; return group.items.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1.创建cell iCocosSettingCell *cell = [iCocosSettingCell cellWithTableView:tableView]; // 取模型 // 哪一组的模型 iCocosGroupItem *group = self.groups[indexPath.section]; // 从模型数组数组中取出对应的模型 iCocosSettingItem *item = group.items[indexPath.row]; // 2.给cell传递模型,给cell的子控件赋值 cell.item = item; return cell; } // 返回第section组的头部标题 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { // 取出当前是哪一组 iCocosGroupItem *group = self.groups[section]; return group.headerTitle; } // 返回第section组的尾部标题 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { // 取出当前是哪一组 iCocosGroupItem *group = self.groups[section]; return group.footerTitle; } #pragma mark - 监听cell点击 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; // 取出模型 iCocosGroupItem *group = self.groups[indexPath.section]; iCocosSettingItem *item = group.items[indexPath.row]; // 判断下有木有事情,就判断下block有没有值 if (item.operationBlock) { // 执行保存的代码 item.operationBlock(); return; } if ([item isKindOfClass:[iCocosSettingArrowItem class]]) { iCocosSettingArrowItem *arrowItem = (iCocosSettingArrowItem *)item; if (arrowItem.descVc) { // 创建目的控制器 UIViewController *vc = [[arrowItem.descVc alloc] init]; vc.navigationItem.title = item.title; // 跳转界面 [self.navigationController pushViewController:vc animated:YES]; } } }
点击对应的行之后就会显示对应的界面:
以后如果需要拓展任何点击进入里面子控件或者孙子空间我们只需要同上面的操作就可以了,其它操作就是一些对应的数据的修改的优化。
最后的封装:基类的实现
最后实现一下基类的抽取,以后需要用的话就更简单了,只需要拷贝到项目类似上面的操作直接使用酒可以
声明一个数组的组模型用来给外部使用
@interface iCocosBaseSettingController : UITableViewController // 总共的组数 @property (nonatomic, strong) NSMutableArray *groups; @end
来看看基类的实现:
#import "iCocosBaseSettingController.h" @interface iCocosBaseSettingController () @end @implementation iCocosBaseSettingController - (instancetype)init { return [self initWithStyle:UITableViewStyleGrouped]; } - (NSMutableArray *)groups { if (_groups == nil) { _groups = [NSMutableArray array]; } return _groups; } #pragma mark - 数据源 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.groups.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 取出当前的组模型 iCocosGroupItem * group = self.groups[section]; return group.items.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1.创建cell iCocosSettingCell *cell = [iCocosSettingCell cellWithTableView:tableView]; // 取模型 // 哪一组的模型 iCocosGroupItem *group = self.groups[indexPath.section]; // 从模型数组数组中取出对应的模型 iCocosSettingItem *item = group.items[indexPath.row]; // 2.给cell传递模型,给cell的子控件赋值 cell.item = item; return cell; } // 返回第section组的头部标题 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { // 取出当前是哪一组 iCocosGroupItem *group = self.groups[section]; return group.headerTitle; } // 返回第section组的尾部标题 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { // 取出当前是哪一组 iCocosGroupItem *group = self.groups[section]; return group.footerTitle; } #pragma mark - 监听cell点击 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; // 取出模型 iCocosGroupItem *group = self.groups[indexPath.section]; iCocosSettingItem *item = group.items[indexPath.row]; // 判断下有木有事情,就判断下block有没有值 if (item.operationBlock) { // 执行保存的代码 item.operationBlock(indexPath); return; } if ([item isKindOfClass:[iCocosSettingArrowItem class]]) { iCocosSettingArrowItem *arrowItem = (iCocosSettingArrowItem *)item; if (arrowItem.descVc) { // 创建目的控制器 UIViewController *vc = [[arrowItem.descVc alloc] init]; vc.navigationItem.title = item.title; // 跳转界面 [self.navigationController pushViewController:vc animated:YES]; } } }
以后使用的话直接将我们创建的类继承子我们基类并且添加对应的行组数据就可以,非常简单:
简单使用:
先来看看简单的使用
- (void)viewDidLoad { [super viewDidLoad]; // 添加第0组 [self setUpGroup0]; } // 添加第0组 - (void)setUpGroup0 { // 创建行模型 // 开奖推送 iCocosSettingArrowItem *item = [iCocosSettingArrowItem itemWithImage:nil title:@"开奖推送"]; item.descVc = [UIViewController class]; // 比分直播 iCocosSettingArrowItem *item1 = [iCocosSettingArrowItem itemWithImage:nil title:@"比分直播"]; item1.descVc = [iCocosScoreViewController class]; // 中奖动画 iCocosSettingArrowItem *item2 = [iCocosSettingArrowItem itemWithImage:nil title:@"中奖动画"]; // 购彩提醒 iCocosSettingArrowItem *item3 = [iCocosSettingArrowItem itemWithImage:nil title:@"购彩提醒"]; // Items:存储当前数组有多少行模型 // 创建一个组模型,描述第0组 iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[item,item1,item2,item3]]; // 添加组模型到groups数组,有多少个组模型就有多少组 [self.groups addObject:group]; }
比如设置界面:
@interface iCocosSettingViewController () @end @implementation iCocosSettingViewController - (void)viewDidLoad { [super viewDidLoad]; // self.navigationItem.title = @"设置"; // 设置导航条的标题 self.title = @"设置"; // 添加第0组 [self setUpGroup0]; // 添加第1组 [self setUpGroup1]; // 添加第2组 [self setUpGroup2]; } // 当一个对象要销毁的时候,就会调用这个方法 - (void)dealloc { NSLog(@"%s",__func__); } // 添加第0组 - (void)setUpGroup0 { // 创建行模型 // 使用兑换码 iCocosSettingArrowItem *RedeemCode = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"]; // RedeemCode.descVc = [UIViewController class]; // 使用block的注意点,尽量避免循环引用 // block会把代码块里面的所有强指针强引用 // 会把当前控制器的对象强引用 // 解决循环引用,用weak // 把self强指针转换为弱指针 // typeof(x) 获取x的类型 iCocosSettingViewController * __weak typeof(self) weakSelf = self; // 在block中最好不要直接访问成员属性 RedeemCode.operationBlock = ^(NSIndexPath *indexPath){ UIViewController *vc = [[UIViewController alloc] init]; vc.view.backgroundColor = [UIColor redColor]; vc.title = @"asldjasd"; [weakSelf.navigationController pushViewController:vc animated:YES]; // self -> _groups NSLog(@"%@",weakSelf.groups); }; // Items:存储当前数组有多少行模型 // 创建一个组模型,描述第0组 iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[RedeemCode]]; // 设置头部标题 group.headerTitle = @"abc"; // 添加组模型到groups数组,有多少个组模型就有多少组 [self.groups addObject:group]; } // 添加第1组 - (void)setUpGroup1 { // 推送和提醒 iCocosSettingArrowItem *push = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"推送和提醒"]; // 设置目的控制器的类名 push.descVc = [iCocosPushViewController class]; // 使用兑换码 iCocosSettingItem *RedeemCode1 = [iCocosSettingSwitchItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"]; // 使用兑换码 iCocosSettingItem *RedeemCode2 = [iCocosSettingSwitchItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"]; // 使用兑换码 iCocosSettingItem *RedeemCode3 = [iCocosSettingSwitchItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"]; // 描述第一组有多少个行模型,描述第1组 NSArray *items = @[push,RedeemCode1,RedeemCode2,RedeemCode3]; // 创建组模型 iCocosGroupItem *group = [iCocosGroupItem groupWithItems:items]; group.headerTitle = @"asd"; group.footerTitle = @"asdasdq"; // 添加到group总数组 [self.groups addObject:group]; } // 添加第2组 - (void)setUpGroup2 { // 使用兑换码 iCocosSettingItem *version = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"检查新版本"]; // 保存检查新版本需要做的事情 version.operationBlock = ^(NSIndexPath *indexPath){ [MBProgressHUD showSuccess:@"没有最新的版本"]; }; // 使用兑换码 iCocosSettingItem *RedeemCode1 = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"]; // 使用兑换码 iCocosSettingItem *RedeemCode2 = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"]; // 使用兑换码 iCocosSettingItem *RedeemCode3 = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"]; // 描述第一组有多少个行模型,描述第1组 NSArray *items = @[version,RedeemCode1,RedeemCode2,RedeemCode3]; // 创建组模型 iCocosGroupItem *group = [iCocosGroupItem groupWithItems:items]; group.footerTitle = @"bcd"; // 添加到group总数组 [self.groups addObject:group]; }
比分直播界面
@interface iCocosScoreViewController () @end @implementation iCocosScoreViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 添加3组 [self setUpGroup0]; [self setUpGroup1]; [self setUpGroup2]; [self setUpGroup2]; [self setUpGroup2]; [self setUpGroup2]; [self setUpGroup2]; } - (void)setUpGroup0 { // 创建行模型 iCocosSettingSwitchItem *item = [iCocosSettingSwitchItem itemWithImage:nil title:@"关注比赛"]; // 创建组模型 iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[item]]; group.footerTitle = @"sadsad"; // 添加groups数组 [self.groups addObject:group]; } - (void)setUpGroup1 { // 创建行模型 iCocosSettingItem *item = [iCocosSettingItem itemWithImage:nil title:@"起始时间"]; item.subTitle = @"00:00"; // 创建组模型 iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[item]]; // 添加groups数组 [self.groups addObject:group]; } - (void)setUpGroup2 { // 创建行模型 iCocosSettingItem *item = [iCocosSettingItem itemWithImage:nil title:@"结束时间"]; item.subTitle = @"23:59"; __weak typeof(self) weakSelf = self; item.operationBlock = ^(NSIndexPath *indexPath){ // 获取选中的cell,把键盘添加到cell上面 UITableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:indexPath]; // 弹出键盘 UITextField *textField = [[UITextField alloc] init]; [textField becomeFirstResponder]; [cell addSubview:textField]; // 在iOS7之后,只要把textField添加到需要弹出键盘的cell上面,就会自动做好键盘处理 }; // 创建组模型 iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[item]]; // 添加groups数组 [self.groups addObject:group]; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self.view endEditing:YES]; } @end
好了终于扯完了,希望对你有用,也方便自己以后复习
最后附上一份封装好的相关文件下载:http://i.cnblogs.com/Files.aspx
iOS开发——开发必备OC篇&UITableView设置界面完整封装(四)的更多相关文章
- iOS开发——开发必备OC篇&UITableView设置界面完整封装(三)
UITableView设置界面完整封装(三) 简单MVC实现UITableView设置界面之界面跳转 创建一个需要调整的对应的控制器 在需要调整的类型模型中创建对应的属性用来实现调整类型控制器的设置 ...
- iOS开发——开发必备OC篇&UITableView设置界面完整封装(二)
UITableView设置界面完整封装(二) 简单MVC实现UITableView设置界面之Cell右边类型设置 首先来看看第一种方法证明使用,结合两种方法之后根据个人的爱好去选择就可以了, 一:使用 ...
- iOS开发——开发必备OC篇&UITableView设置界面完整封装(一)
UITableView设置界面完整封装(一) 简单MVC实现UITableView设置界面分组数据显示 一:模型 1:cell模型 /** 描述cell图片 */ @property (nonatom ...
- ios开发——实用技术篇OC篇&iOS的主要框架
iOS的主要框架 阅读目录 Foundation框架为所有的应用程序提供基本系统服务 UIKit框架提供创建基于触摸用户界面的类 Core Data框架管着理应用程序数据模型 Core ...
- iOS应用 跳转到系统的设置界面
现在很多APP都需要获取用户权限,例如,允许调用位置信息,读取短信,拨打电话,开启WIFI,掉头摄像头等,用户不允许APP获取这些权限的时候.最好的用户体验是,直接跳转到系统设置界面,让用户自己设置. ...
- iOS开发——UI篇OC篇&UITableView简单封装
UITableView简单封装 UITableView时iOS开发中使用最多也是最重的一个UI空间,其实在App Store里面的%80以上的应用都用到了这个控件,所以就给大家介绍一下,前面的文章中也 ...
- iOS开发——开发必备OC篇&彩票实战之精华讲解
彩票实战之精华讲解 在这段时间自己研究并学习关于彩票项目开发的时候遇到各种坑,各种技术点以前或许之前用过但是却用起来不是那么熟悉,所以没遇到一个重点的地方我就会记录一下,希望不会再有下次. 本文主要讲 ...
- iOS开发——UI篇OC篇&UITableView多项选择
UITableView多项选择 自定义cell和取到相应的cell就行了 TableViewCell.h #import <UIKit/UIKit.h> @interface TableV ...
- iOS开发——网络实用技术OC篇&网络爬虫-使用青花瓷抓取网络数据
网络爬虫-使用青花瓷抓取网络数据 由于最近在研究网络爬虫相关技术,刚好看到一篇的的搬了过来! 望谅解..... 写本文的契机主要是前段时间有次用青花瓷抓包有一步忘了,在网上查了半天也没找到写的完整的教 ...
随机推荐
- 最好的10本适用于初学者阅读的javaScript和jQuery书籍
现在有许多方式学习新的内容,但是对于刚开始学习和寻找真正沉迷其中的平和状态的人而言,经典的纸质书籍依旧是首选.我发现阅读一本纸质书籍会让自己远离那些在使用电脑和平板时出现的令人不安的情绪.电子书和播客 ...
- 使用JavaMail API发送邮件
发送邮件是很常用的功能,注册验证,找回密码,到货通知,欠费提醒等,都可以通过邮件来提醒. Java中发送邮件需要使用javax.mail.jar包,读者可以上网搜索或去官方下载,下载地址为: 下面贴上 ...
- Hadoop学习记录(7)|Eclipse远程调试Hadoop
1.创建Hadoop项目 2.创建包.类 这里使用hdfs.WordCount为例 3.编写自定Mapper和Reducer程序 MyMapper类 static class MyMapper ext ...
- nodejs cmd
引入系统模块 var exec = require('child_process').exec; cmd.js var exec = require('child_process').exec; va ...
- 怎么从sqlserver 数据库导出 insert 的数据语句
In SSMS in the Object Explorer, right click on the database right-click and pick "Tasks" a ...
- 第二百二十三天 how can I 坚持
今天双十一,过得有点郁闷,昨天鱼死了不说,抢的羽绒服今天才发现是棉服,结果又买了个海澜之家的,搞的今天凌晨买的东西全都写退了,除了小米耳机. 光棍节,好纠结.爱要怎么说出口,你才不会拒绝啊,愁人啊. ...
- linux 分区与挂载
流程:分区-格式化-挂载 1.添加硬盘 使用fdisk -l命令查看磁盘状态 此处可以看到两块硬盘sda和sdb,第一块硬盘sda是装好系统的.sdb硬盘是未进行分区的. 本例将这个20G的硬盘分区, ...
- NSInvocation Basics
In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Appl ...
- VS2008注册码
PYHYP-WXB3B-B2CCM-V9DX9-VDY8T 如果下载的是90天的试用版,下载下来以后把ISO里面的Setup\setup.sdb文件用记事本打开,把其中的[Product Key]下面 ...
- 动态链接--so的搜索过程
可执行文件所依赖的so路径保存在.dynamic 里面,由DT_NEED类型表示.如下: 如果DT_NEED里面保存的是绝对路径,那ld就在绝对路径下查找so. 如果DT_NEED里面保存的是相对路径 ...