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篇&网络爬虫-使用青花瓷抓取网络数据
网络爬虫-使用青花瓷抓取网络数据 由于最近在研究网络爬虫相关技术,刚好看到一篇的的搬了过来! 望谅解..... 写本文的契机主要是前段时间有次用青花瓷抓包有一步忘了,在网上查了半天也没找到写的完整的教 ...
随机推荐
- python 数字和字符串转换问题
一.python中字符串转换成数字 (1)import string tt='555' ts=string.atoi(tt) ts即为tt转换成的数字 转换为浮点数 string.atof(tt) ( ...
- 基于select模型的udp客户端实现超时机制
参考:http://www.cnblogs.com/chenshuyi/p/3539949.html 多路选择I/O — select模型 其思想在于使用一个集合,该集合中包含需要进行读写的fd,通过 ...
- 牛课--C/C++
引用是除指针外另一个可以产生多态效果的手段. //引用是除指针外另一个可以产生多态效果的手段. #include<iostream> using namespace std; class ...
- 编写服务说明.thrift文件
1.数据类型 基本类型: bool:布尔值,true 或 false,对应 Java 的 boolean byte:8 位有符号整数,对应 Java 的 byte i16:16 位有符号整数,对应 J ...
- 理解display:inline、block、inline-block
要理解display:inline.block.inline-block的区别,需要先了解HTML中的块级(block)元素和行级(inline)元素的特点,行内元素也叫内联元素. 块级元素 总是另起 ...
- Linker scripts之MEMORY
1 MEMORY command The MEMORY command describes the location and size of blocks of memory in the targe ...
- 《学习OpenCV》练习题第四章第一题b&c
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
- IOS中 什么是UITableView的索引放大镜字符
IOS中 什么是UITableView的索引放大镜字符 [_dataSource addObject:UITableViewIndexSearch]; 版权声明:本文为博主原创文章,未经博主允许不得转 ...
- 2016 ACM/ICPC 沈阳站 小结
铜铜铜…… 人呐真奇怪 铁牌水平总想着运气好拿个铜 铜牌水平总想着运气好拿个银 估计银牌的聚聚们一定也不满意 想拿个金吧 这次比赛挺不爽的 AB两道SB题,十分钟基本全场都过了 不知道出这种题有什么意 ...
- 安装Win7和Office2010并激活
1. 下载Win7 建议安装原版的win7 SP1 64位中文旗舰版,不建议安装Ghost版本,之前用U盘安装Ghost版本一直失败.原版的下载地址为,选其中一个地址下载就行了. ed2k://|fi ...