UITableView设置界面完整封装(一)

简单MVC实现UITableView设置界面分组数据显示

一:模型

1:cell模型

 /** 描述cell图片 */

 @property (nonatomic, strong) UIImage *image;

 /** 描述cell文字 */

 @property (nonatomic, strong) NSString *title;
 

// 根据行模型确定cell右边辅助视图

// 1.提供一个类型枚举,箭头,开头

// 2.用子类去判断cell的类型

+ (instancetype)itemWithImage:(UIImage *)image title:(NSString *)title;

实现文件

 + (instancetype)itemWithImage:(UIImage *)image title:(NSString *)title

 {

     iCocosSettingItem *item = [[self alloc] init];

     item.image = image;

     item.title = title;

     return item;

 }

2:分组模型

 /** 描述当前组有多少行 */

 @property (nonatomic, strong) NSArray *items;

 /** 头部标题 */

 @property (nonatomic, strong) NSString *headerTitle;

 /** 尾部标题 */

 @property (nonatomic, strong) NSString *footerTitle;

 + (instancetype)groupWithItems:(NSArray *)items;

实现文件

 + (instancetype)groupWithItems:(NSArray *)items

 {

     iCocosGroupItem *group = [[self alloc] init];

     group.items = items;

     return group;

 }

二:视图

 @interface iCocosSettingCell : UITableViewCell

 + (instancetype)cellWithTableView:(UITableView *)tableView;

 /** item行模型,描述cell的外观 */

 @property (nonatomic, strong) iCocosSettingItem *item;

实现文件

 + (instancetype)cellWithTableView:(UITableView *)tableView

 {

     static NSString *ID = @"cell";

     XMGSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

     if (cell == nil) {

         cell = [[iCocosSettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

     }

     return cell;

 }

 - (void)setItem:(iCocosSettingItem *)item

 {

     _item = item;

     // 设置子控件数据

     [self setUpData];

     // 设置辅助视图

     [self setUpAccessoryView];

 }

 #pragma mark - 设置辅助视图

 - (void)setUpData

 {

     self.imageView.image = _item.image;

     self.textLabel.text = _item.title;

 }

 #pragma mark - 设置辅助视图

 - (void)setUpAccessoryView

 {

     // 设置辅助视图

     if ([_item isKindOfClass:[iCocosSettingArrowItem class]]) {

         // 展示箭头

         UIImageView *arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right"]];

         self.accessoryView = arrowView;

     }else if ([_item isKindOfClass:[iCocosSettingSwitchItem class]]){

         // 展示开关

         UISwitch *switchView = [[UISwitch alloc] init];

         self.accessoryView = switchView;

     }else{

         self.accessoryView = nil;

     }

 }

三:控制器


 @interface iCocosSettingViewController ()

 // 总共的组数

 @property (nonatomic, strong) NSMutableArray *groups;

 @end

 @implementation iCocosSettingViewController

 - (NSMutableArray *)groups

 {

     if (_groups == nil) {

         _groups = [NSMutableArray array];

     }

     return _groups;

 }

 - (instancetype)init

 {

     return [self initWithStyle:UITableViewStyleGrouped];

 }

 - (void)viewDidLoad {

     [super viewDidLoad];

     // 添加第0组

     [self setUpGroup0];

     // 添加第1组

     [self setUpGroup1];

     // 添加第2组

     [self setUpGroup2];

 }

 // 添加第0组

 - (void)setUpGroup0

 {

     // 创建行模型

     // 使用兑换码

     iCocosSettingItem *RedeemCode = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];

     // Items:存储当前数组有多少行模型

     // 创建一个组模型,描述第0组

     iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[RedeemCode]];

     // 设置头部标题

     group.headerTitle = @"abc";

     // 添加组模型到groups数组,有多少个组模型就有多少组

     [self.groups addObject:group];

 }

 // 添加第1组

 - (void)setUpGroup1

 {

     // 使用兑换码

     iCocosSettingItem *RedeemCode = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];

     // 使用兑换码

     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 = @[RedeemCode,RedeemCode1,RedeemCode2,RedeemCode3];

     // 创建组模型

     iCocosGroupItem *group = [iCocosGroupItem groupWithItems:items];

     group.headerTitle = @"asd";

     group.footerTitle = @"asdasdq";

     // 添加到group总数组

     [self.groups addObject:group];

 }

 // 添加第2组

 - (void)setUpGroup2

 {

     // 使用兑换码

     iCocosSettingItem *RedeemCode = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];

     // 使用兑换码

     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 = @[RedeemCode,RedeemCode1,RedeemCode2,RedeemCode3];

     // 创建组模型

     iCocosGroupItem *group = [iCocosGroupItem groupWithItems:items];

     group.footerTitle = @"bcd";

     // 添加到group总数组

     [self.groups addObject:group];

 }

 #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;

 }

 @end

最终的现实界面

iOS开发——开发必备OC篇&UITableView设置界面完整封装(一)的更多相关文章

  1. iOS开发——开发必备OC篇&UITableView设置界面完整封装(三)

    UITableView设置界面完整封装(三) 简单MVC实现UITableView设置界面之界面跳转 创建一个需要调整的对应的控制器 在需要调整的类型模型中创建对应的属性用来实现调整类型控制器的设置 ...

  2. iOS开发——开发必备OC篇&UITableView设置界面完整封装(二)

    UITableView设置界面完整封装(二) 简单MVC实现UITableView设置界面之Cell右边类型设置 首先来看看第一种方法证明使用,结合两种方法之后根据个人的爱好去选择就可以了, 一:使用 ...

  3. iOS开发——开发必备OC篇&UITableView设置界面完整封装(四)

    设置界面完整封装(四) 简单MVC实现UITableView设置界面完善封装及拓展使用 关于使用和拓展, 其实基本上就是同UItableView,知识讲数据改一下就可以 拓展使用 1:首先定义一个数组 ...

  4. ios开发——实用技术篇OC篇&iOS的主要框架

    iOS的主要框架         阅读目录 Foundation框架为所有的应用程序提供基本系统服务 UIKit框架提供创建基于触摸用户界面的类 Core Data框架管着理应用程序数据模型 Core ...

  5. iOS应用 跳转到系统的设置界面

    现在很多APP都需要获取用户权限,例如,允许调用位置信息,读取短信,拨打电话,开启WIFI,掉头摄像头等,用户不允许APP获取这些权限的时候.最好的用户体验是,直接跳转到系统设置界面,让用户自己设置. ...

  6. iOS开发——UI篇OC篇&UITableView简单封装

    UITableView简单封装 UITableView时iOS开发中使用最多也是最重的一个UI空间,其实在App Store里面的%80以上的应用都用到了这个控件,所以就给大家介绍一下,前面的文章中也 ...

  7. iOS开发——开发必备OC篇&彩票实战之精华讲解

    彩票实战之精华讲解 在这段时间自己研究并学习关于彩票项目开发的时候遇到各种坑,各种技术点以前或许之前用过但是却用起来不是那么熟悉,所以没遇到一个重点的地方我就会记录一下,希望不会再有下次. 本文主要讲 ...

  8. iOS开发——UI篇OC篇&UITableView多项选择

    UITableView多项选择 自定义cell和取到相应的cell就行了 TableViewCell.h #import <UIKit/UIKit.h> @interface TableV ...

  9. iOS开发——网络实用技术OC篇&网络爬虫-使用青花瓷抓取网络数据

    网络爬虫-使用青花瓷抓取网络数据 由于最近在研究网络爬虫相关技术,刚好看到一篇的的搬了过来! 望谅解..... 写本文的契机主要是前段时间有次用青花瓷抓包有一步忘了,在网上查了半天也没找到写的完整的教 ...

随机推荐

  1. Qt之操作数据库(SQLite)

    SQLite 简介 SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需 ...

  2. Yarn中的几种状态机

    1 概述 为了增大并发性,Yarn采用事件驱动的并发模型,将各种处理逻辑抽象成事件和调度器,将事件的处理过程用状态机表示.什么是状态机? 如果一个对象,其构成为若干个状态,以及触发这些状态发生相互转移 ...

  3. Visual Studio Profiler 跟踪检查每个exe dll 性能 执行时间 CPU占用情况的方法

  4. 在linnux下,配置自动备份oacle

    以oracle身份登录到linux,在oracle home目录下创建目录 shell $ mkdir shell 创建自动备份脚本 $ cd shell $ touch expdp.sh $ chm ...

  5. Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法

    在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或者在iframe框架中使用父窗口的元素 js 在父窗口中获取iframe中的元素  1. 格式:window ...

  6. KextWizard 的使用方法;以及Kext安装的几种工具下载

    a.将你需要安装的Kext拖到非中文的路径中: b.运行该软件,将Kext拖入下图对应的方框里,然后选择位置安装: c.选择修复权限和重建缓存(一个是修复Extra文件夹,一个是修复SLE) Kext ...

  7. find命令之xargs

    在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出 ...

  8. iOS开发中的测试框架

    转载作者:@crespoxiao 我们为什么要用测试框架呢?当然对项目开发有帮助了,但是业内现状是经常赶进度,所以TDD还是算了吧,BDD就测测数据存取和重要环节,这很重要,一次性跑完测试单元检查接口 ...

  9. MetaQ安装部署文档

    一.MetaQ安装部署情况: 地点 IP Broker ID Master/Slave Slave ID:Group 合肥 192.168.52.23 Slave 1:meta-slave-group ...

  10. Computational Geometry Template_Polygon

    #include <stdlib.h> #include <math.h> #include <iostream> #define MAXN 1000 #defin ...