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篇&网络爬虫-使用青花瓷抓取网络数据
网络爬虫-使用青花瓷抓取网络数据 由于最近在研究网络爬虫相关技术,刚好看到一篇的的搬了过来! 望谅解..... 写本文的契机主要是前段时间有次用青花瓷抓包有一步忘了,在网上查了半天也没找到写的完整的教 ...
随机推荐
- 自建存储与使用微软Azure、七牛等第三方云存储综合考察分析
http://www.cnblogs.com/sennly/p/4136734.html 各种云服务这两年炒的火热,加之可以降低成本,公司想先在部分业务上尝试使用下,刚好最近有个项目有大量小文件需要存 ...
- QCon2013上海站总结 -- 前端开发
选择这个专题开始主要有两个原因:一是这次会议关于前端开发的内容不多.二是我做过几年前端开发,这个专题对我来说会容易点:) 这次QCon上海关于前端开发有一个Keynote,一个Javascript专题 ...
- reds pub/sub官方文档翻译
Publish / Subscribe发布/订阅 redis-py includes a PubSub object that subscribes to channels and listens f ...
- Hadoop构成
What Is Apache Hadoop? The Apache™ Hadoop® project develops open-source software for reliable, scala ...
- LINQ标准查询操作符(二)——Join、GroupJoin、GroupBy、Concat、
四.联接操作符 联接是指将一个数据源对象与另一个数据源对象进行关联或者联合的操作.这两个数据源对象通过一个共同的值或者属性进行关联. LINQ有两个联接操作符:Join和GroupJoin. 1. J ...
- ASP.NET自定义控件入门Demo
最近看了MSDN关于自定义控件的介绍,根据官方的文档,自己学着做了一个简单的Demo给需要的朋友参考. ASP.NET 源生的TextBox是不带Label标签的,这里我要实现的是创建一个带Label ...
- ZOJ 3810 Pretty Poem 分类: ACM 2015-05-17 14:40 83人阅读 评论(0) 收藏
Pretty Poem Time Limit: 2 Seconds Memory Limit:65536 KB Poetry is a form of literature that uses ...
- webservice接口的发布
使用xfire-client发布webservice接口: commons-codec-1.3.jar commons-httpclient-3.0.jar 在src 下创建META-INF/xfir ...
- c++中从一段字符串中提取数字
采用标准输入输出: 输入:12&3 34*133^3131 13031* 输出:12 3 34 133 3131 13031 思路,先将整个输入存进一个字符串,再解析字符串,这样运行速度会 ...
- 使用spring.net 1.3.2框架部署在虚拟目录上发生错误
如果你的网站使用了Spring.net 1.3.2,并部署在IIS的虚拟目录上,那么将会出现如下错误: The virtual path '/currentcontext.dummy' maps ...