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篇&网络爬虫-使用青花瓷抓取网络数据
网络爬虫-使用青花瓷抓取网络数据 由于最近在研究网络爬虫相关技术,刚好看到一篇的的搬了过来! 望谅解..... 写本文的契机主要是前段时间有次用青花瓷抓包有一步忘了,在网上查了半天也没找到写的完整的教 ...
随机推荐
- C#语言基础02
字符串:string s="ab";string s1="a\nb";//n:newline或者next的意思. string s="a\\b&quo ...
- 调用openoffice Com组件转换成PDF
//例子 OpenOfficeExportToPDF('file:///C:/SourceFile.odt', 'file:///C:/TargetFile.pdf'); 必须为URL格式的路径 ...
- java 的开源wiki维基系统
几乎所有 维基 系统的对比网址: http://www.wikimatrix.org/ XWiki, 第二代wiki. 它里面使用的 velocity 模板语言对j2ee开发相当有参考价值, ...
- [翻译][Trident] Trident state原理
原文地址:https://github.com/nathanmarz/storm/wiki/Trident-state ----------------------------- Trident在读写 ...
- FZU 8月有奖月赛A Daxia & Wzc's problem (Lucas)
Problem A Daxia & Wzc's problem Accept: 42 Submit: 228Time Limit: 1000 mSec Memory Limit : ...
- Codeforces 597C. Subsequences (树状数组+dp)
题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...
- URAL 2067 Friends and Berries (推理,数学)
题意:给定 n 个人,每个人两个值s, r,要满足,p(v, u) = sqrt((sv − su)^2 + (rv − ru)^2), p(v,u,w) = (p(v,u) + p(v,w) + p ...
- hdoj 5402 Travelling Salesman Problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402 类似于黑白棋盘,有的格子是可以不走的,有的格子是不能不走的,对于m或n中有一个奇数的情况, 所有 ...
- 用vagrant搭建一个自己的lnmp环境(一)
用vagrant搭建自己的lnmp环境 1.工具: a.vagrant b.virtual box c.linux服务器box(此处我使用centos 7.0) 2.安装完vagrant和virtua ...
- GotGitHub
github在线教程 http://www.worldhello.net/gotgithub/