一、介绍

在iOS开发中,tableView非常常用,能将其展示出来,它的数据源必不可少。当然数据源有动态下发的,有固定写死的,这里我只探讨固定写死的情况。对于死数据,我们在项目中经常遇到的场景就是我的模块,以及设置模块等。那么,这些死数据我们如何组装的呢?  在以前开发中,我直接用一个可变数组装着每一个cell对应的字典(字典中包含每一个cell需要字段的键值对),虽然也可以实现效果,但是扩展不方便,本人不推荐。 在开发中,我的搭档推荐我项目中的封装的cell模型,我一看,确实不错,在这里给大家分享一下。

二、思想

1、定义继承NSObject的CustomCellConfig类,定义cell可能需要的全部属性

2、定义一个实例化的方法,参数为字典

3、在实例化方法中接着通过OC的setValuesForKeysWithDictionary方法将对应的所有赋值的属性初始化

4、当然再重写一下该类的init方法,默认赋值cell的高为44.0f像素

5、在控制器中创建CustomCellConfig的实体对象并存入数据源,最后刷新tableView

6、获取CustomCellConfig实体对象对cell进行赋值并触发block即可

三、代码

  1. //
  2. // CustomCellConfig.h
  3. // CustomCellConfig
  4. //
  5. // Created by 夏远全 on 2017/12/8.
  6. // Copyright © 2017年 夏远全. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import <UIKit/UIKit.h>
  11.  
  12. @class CustomCellConfig;
  13.  
  14. typedef void (^CustomCellSelectedBlock)(CustomCellConfig *config);
  15.  
  16. @interface CustomCellConfig : NSObject
  17.  
  18. @property (nonatomic, strong) NSString *title;
  19. @property (nonatomic, strong) NSString *detail;
  20. @property (nonatomic, strong) UIImage *image;
  21. @property (nonatomic, strong) NSString *avatar;
  22.  
  23. @property (nonatomic, assign) int badge;
  24. @property (nonatomic, assign) int contentType;
  25. @property (nonatomic, assign) float height;
  26. @property (nonatomic, assign) BOOL canEdit;
  27.  
  28. @property (nonatomic, assign) UITableViewCellAccessoryType accessoryType;
  29. @property (nonatomic, copy) CustomCellSelectedBlock block;
  30. @property (nonatomic, strong) NSString *viewController;
  31. @property (nonatomic, strong) NSString *segue;
  32. @property (nonatomic, strong) NSIndexPath *indexPath;
  33. @property (nonatomic, copy) NSAttributedString *attributedDetail;
  34.  
  35. @property (nonatomic, strong) NSString *identify;
  36. @property (nonatomic, strong) id object;
  37.  
  38. + (CustomCellConfig *)configWithDictionary:(NSDictionary *)dict;
  39.  
  40. @end
  1. //
  2. // CustomCellConfig.m
  3. // CustomCellConfig
  4. //
  5. // Created by 夏远全 on 2017/12/8.
  6. // Copyright © 2017年 夏远全. All rights reserved.
  7. //
  8.  
  9. #import "CustomCellConfig.h"
  10.  
  11. @implementation CustomCellConfig
  12.  
  13. - (id)init {
  14. if (self = [super init]) {
  15. self.height = 44.0f;
  16. }
  17. return self;
  18. }
  19.  
  20. + (CustomCellConfig *)configWithDictionary:(NSDictionary *)dict {
  21. CustomCellConfig *config = [[CustomCellConfig alloc] init];
  22. [config setValuesForKeysWithDictionary:dict];
  23. return config;
  24. }
  25.  
  26. @end

四、使用

  1. //
  2. // ViewController.m
  3. // CustomCellConfig
  4. //
  5. // Created by 夏远全 on 2017/12/8.
  6. // Copyright © 2017年 夏远全. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "CustomCellConfig.h"
  11.  
  12. @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
  13. @property (strong,nonatomic)UITableView *tableView;
  14. @property (strong,nonatomic)NSMutableArray *dataSource;
  15. @end
  16.  
  17. @implementation ViewController
  18.  
  19. #pragma mark - life cycle
  20.  
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23.  
  24. [self setupNavigation];
  25. [self setupDefaultValue];
  26. [self setupSubviews];
  27.  
  28. [self loadData];
  29. }
  30.  
  31. -(void)viewWillAppear:(BOOL)animated
  32. {
  33. [super viewWillAppear:animated];
  34. }
  35.  
  36. -(void)viewWillDisappear:(BOOL)animated
  37. {
  38. [super viewWillDisappear:animated];
  39. }
  40.  
  41. -(void)setupNavigation
  42. {
  43. self.title = @"测试cellConfig";
  44. }
  45.  
  46. -(void)setupDefaultValue
  47. {
  48. self.tableView.backgroundColor = [UIColor whiteColor];
  49. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  50. }
  51.  
  52. -(void)setupSubviews
  53. {
  54. [self addSubViews];
  55. [self setupSubviewsConstraints];
  56. }
  57.  
  58. #pragma mark - add subviews
  59.  
  60. -(void)addSubViews
  61. {
  62. [self.view addSubview:self.tableView];
  63. }
  64.  
  65. #pragma mark - layout subviews
  66.  
  67. -(void)setupSubviewsConstraints
  68. {
  69.  
  70. }
  71.  
  72. #pragma mark - load data
  73. -(void)loadData
  74. {
  75.  
  76. __weak typeof(self) weakSelf = self;
  77.  
  78. //第一组cell
  79. CustomCellConfig *cellConfig1 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"消息",@"accessoryType":@(UITableViewCellAccessoryDisclosureIndicator),@"block":^(CustomCellConfig *config){
  80.  
  81. [weakSelf pushViewController:config];
  82.  
  83. }}];
  84.  
  85. CustomCellConfig *cellConfig2 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"成绩",@"detail":@"分析",@"accessoryType":@(UITableViewCellAccessoryDisclosureIndicator),@"block":^(CustomCellConfig *config){
  86.  
  87. [weakSelf pushViewController:config];
  88.  
  89. }}];
  90. CustomCellConfig *cellConfig3 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"管理",@"attributedDetail":@"",@"accessoryType":@(UITableViewCellAccessoryDisclosureIndicator),@"block":^(CustomCellConfig *config){
  91.  
  92. [weakSelf pushViewController:config];
  93.  
  94. }}];
  95. NSArray<CustomCellConfig *> *section1 = @[cellConfig1,cellConfig2,cellConfig3];
  96.  
  97. //第二组cell
  98. CustomCellConfig *cellConfig4 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"消息",@"height":@(),@"accessoryType":@(UITableViewCellAccessoryNone),@"block":^(CustomCellConfig *config){
  99.  
  100. [weakSelf pushViewController:config];
  101.  
  102. }}];
  103. CustomCellConfig *cellConfig5 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"成绩",@"height":@(),@"detail":@"分析",@"accessoryType":@(UITableViewCellAccessoryDetailDisclosureButton),@"block":^(CustomCellConfig *config){
  104.  
  105. [weakSelf pushViewController:config];
  106.  
  107. }}];
  108. CustomCellConfig *cellConfig6 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"管理",@"height":@(),@"accessoryType":@(UITableViewCellAccessoryCheckmark),@"block":^(CustomCellConfig *config){
  109.  
  110. [weakSelf pushViewController:config];
  111.  
  112. }}];
  113. CustomCellConfig *cellConfig7 = [CustomCellConfig configWithDictionary:@{@"image":[UIImage imageNamed:@"avatar"],@"title":@"更多",@"height":@(),@"accessoryType":@(UITableViewCellAccessoryDetailButton),@"block":^(CustomCellConfig *config){
  114.  
  115. [weakSelf pushViewController:config];
  116.  
  117. }}];
  118. NSArray<CustomCellConfig *> *section2 = @[cellConfig4,cellConfig5,cellConfig6,cellConfig7];
  119.  
  120. //......等等......该模型的字段均可用,自己根据需要进行赋值...........//
  121.  
  122. [self.dataSource addObject:section1];
  123. [self.dataSource addObject:section2];
  124. [self.tableView reloadData];
  125. }
  126.  
  127. #pragma mark - delegate - datasource methods
  128. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  129. {
  130. return self.dataSource.count;
  131. }
  132.  
  133. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  134. {
  135. return [self.dataSource[section] count];
  136. }
  137.  
  138. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  139.  
  140. static NSString *reuserIdentifier = @"reuserIdentifier";
  141. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuserIdentifier];
  142. if (!cell) {
  143. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserIdentifier];
  144. }
  145. CustomCellConfig *cellConfig = self.dataSource[indexPath.section][indexPath.row];
  146. cell.imageView.image = cellConfig.image;
  147. cell.textLabel.text = cellConfig.title;
  148. cell.detailTextLabel.text = cellConfig.detail;
  149. cell.accessoryType = cellConfig.accessoryType;
  150. return cell;
  151. }
  152.  
  153. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  154. CustomCellConfig *cellConfig = self.dataSource[indexPath.section][indexPath.row];
  155. return cellConfig.height;
  156. }
  157.  
  158. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  159.  
  160. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  161.  
  162. CustomCellConfig *cellConfig = self.dataSource[indexPath.section][indexPath.row];
  163. if (cellConfig.block) {
  164. cellConfig.block(cellConfig);
  165. }
  166. }
  167.  
  168. #pragma mark - event rsponse
  169. -(void)pushViewController:(CustomCellConfig *)cellConfig{
  170.  
  171. UIViewController *vc = [[UIViewController alloc] init];
  172. vc.view.backgroundColor = [UIColor redColor];
  173. [self.navigationController pushViewController:vc animated:YES];
  174. }
  175.  
  176. #pragma mark - public methods
  177.  
  178. #pragma mark - private methods
  179.  
  180. #pragma mark - getters and setters
  181. -(UITableView *)tableView
  182. {
  183. if (!_tableView) {
  184. _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
  185. _tableView.dataSource = self;
  186. _tableView.delegate = self;
  187. }
  188. return _tableView;
  189. }
  190. -(NSMutableArray *)dataSource{
  191. if (!_dataSource) {
  192. _dataSource = [NSMutableArray array];
  193. }
  194. return _dataSource;
  195. }
  196.  
  197. @end

五、效果

iOS:针对固定数据源,更好的封装cell的更多相关文章

  1. iOS开发之微信聊天工具栏的封装

    之前山寨了一个新浪微博(iOS开发之山寨版新浪微博小结),这几天就山寨个微信吧.之前已经把微信的视图结构简单的拖了一下(IOS开发之微信山寨版),今天就开始给微信加上具体的实现功能,那么就先从微信的聊 ...

  2. wzplayer for ios 针对(mms)优化版本V1.0

    wzplayer for ios针对mms优化版本发布. 1.支持mms,http,rtmp,rtsp等协议 2.支持全格式 下载地址:http://www.coolradio.cn/WzPlayer ...

  3. web自动化针对PO模式进行二次封装之basepage

    在PO模式当中,我们做到了页面对象与测试用例的分离,但在页面对象编写时,我们仍然还有优化的空间.页面对象有一些共同的基本操作,可以封装起来,并可以在基本操作当中加上日志和异常截图的处理.比如说我们在查 ...

  4. iOS开发UI篇—核心动画(UIView封装动画)

    iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...

  5. APP自动化针对PO模式进行二次封装之basepage

    APP自动化跟WEB自动化所使用的框架基本一样,都是采用的PO模式结合pytest框架编写自动化测试脚本,为了提高代码的复用性.稳定性和易维护性,我们针对PO模式进行了二次封装,将日志,等待以及异常截 ...

  6. iOS开发之自定义表情键盘(组件封装与自动布局)

    下面的东西是编写自定义的表情键盘,话不多说,开门见山吧!下面主要用到的知识有MVC, iOS开发中的自动布局,自定义组件的封装与使用,Block回调,CoreData的使用.有的小伙伴可能会问写一个自 ...

  7. iOS数据库离线缓存思路和网络层封装

    一直想总结一下关于iOS的离线数据缓存的方面的问题,然后近期也简单的对AFN进行了再次封装.全部想把这两个结合起来写一下.数据展示型的页面做离线缓存能够有更好的用户体验,用户在离线环境下仍然能够获取一 ...

  8. 让iOS开发变得更有效率-分类、工具类

    在工作中整理的一些分类与工具类,分享给大家.这些工具类可以减少项目中的代码量,让代码变得更简洁,可以大大的提升项目的效率,直接拖到项目中使用即可.下载地址:https://github.com/lee ...

  9. iOS菜鸟之FMDB的二次封装简单易用

    闲来无事写点东西,希望大家多多指正! 大家先去git下载FMDB,然后将其中source文件夹中的fmdb文件夹拖入自己的项目中.最后就可以引用下面的代码对fmdb进行一次简单的封装. 这样可以更直观 ...

随机推荐

  1. OCM_第十四天课程:Section6 —》数据库性能调优_各类索引 /调优工具使用/SQL 优化建议

    注:本文为原著(其内容来自 腾科教育培训课堂).阅读本文注意事项如下: 1:所有文章的转载请标注本文出处. 2:本文非本人不得用于商业用途.违者将承当相应法律责任. 3:该系列文章目录列表: 一:&l ...

  2. MySQL CPU 使用率高的原因和解决方法

    用户在使用 MySQL 实例时,会遇到 CPU 使用率过高甚至达到 100% 的情况.本文将介绍造成该状况的常见原因以及解决方法,并通过 CPU 使用率为 100% 的典型场景,来分析引起该状况的原因 ...

  3. C++ code:位操作实例(bit operation example)

    某任务需要在A.B.C.D.E这五个人中物色人员去完成,但派人受限于下列条件: (1)若A去,则B跟去 (2)D,E两人中必有人去 (3)B,C两人中必有人去,但只去一人 (4)C,D两人要么都去,要 ...

  4. C++ code:More Loop Designs

    1  逻辑判断 对于逻辑判断问题,一般都要考虑全部的可能性,然后从这些可能性中按条件逐一排查,直到最后获得某个结论. [百钱买百鸡问题] 问题描述: 雄鸡(cock)7元一只,母鸡(hen)5元一只, ...

  5. SpringMVC MongoDB之“基本文档查询(Query、BasicQuery)”

    一.简介 spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...

  6. eclipse中Ruby环境搭建

    用Eclipse学习Watir.Eclipse支持Ruby的插件:RDT(Ruby Development Tools),下载下来试用了一下,感觉还是不错的.第一步:获取RDT,通过以下链接可以获得R ...

  7. Promise in Chakra

    http://www.ecma-international.org/ecma-262/#sec-fulfillpromise 25.4.1.3.1 and 25.4.1.3.2 Promise Rej ...

  8. 微信小程序~wx.getUserInfo逐渐废弃,小程序登录过程将如何优化?

    很多的时候我们在做小程序应用的时候,希望用户在使用小程序前进行登录授权,之前登录后通过wx.getUserInfo直接弹出授权的登录方式官方的意思是将不再支持,而是让用户通过下面的方式授权用户信息 & ...

  9. nexus 2版本的配置要点

    nexus 3版本,集成了太多容器化功能,暂时不需要用. 现在主要关注nexus2版本. https://help.sonatype.com/repomanager2/download https:/ ...

  10. 关键字super和this的使用及区别

    "this"作为一个特殊的关键字,它的规则如下: 1.可以表示构造函数传递.this(a,b)表示调用另外一个构造函数.这里面的this就是一个特殊语法,不是变量,没有什么类型. ...