今天看了一些博客文章分享了如何给ViewController 瘦身的问题, 其中一个就是tableView.

的确,随着产品迭代,VC里面可能越来越臃肿,有时候真的需要好好进行一次瘦身.可能是参考的博客中讲解更侧重于方法的复用,其实这个真的是很灵活的问题,有时候tableView list 都是同一规格就比较适合可复用的方法去实现,有时候就是需要单独自定义的,一个tableView 可能有不止两到三个cell 类型的情况. 所以针对后者.我也仿照着写了一个 把 UITableViewDataSource UITableViewDelegate 都提取出来单独做一个对象做处理.

需求:一个tableView 里面目前元素是 一行banner  一行时间  若干行list 元素组成

效果:

SectionDemo 如下:

  1. //
  2. // ViewController减负.h
  3. // 链式DSL学习
  4. //
  5. // Created by HF on 17/3/8.
  6. // Copyright © 2017年 HF-Liqun. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface ViewController__ : UIViewController
  12.  
  13. @property (nonatomic, strong) UITableView *tableView;
  14.  
  15. @end
  16. //
  17. // ViewController减负.m
  18. // 链式DSL学习
  19. //
  20. // Created by HF on 17/3/8.
  21. // Copyright © 2017年 HF-Liqun. All rights reserved.
  22. //
  23.  
  24. #import "ViewController减负.h"
  25. #import "HomeBannerCell.h"
  26. #import "HomeTableViewDataSource.h"
  27.  
  28. @interface ViewController__ ()
  29.  
  30. @property (nonatomic, strong) HomeTableViewDataSource *tabbleViewDataSource;
  31. @end
  32.  
  33. @implementation ViewController__
  34.  
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. [self.view addSubview:self.tableView];
  38. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.bottom.equalTo(self.view);
  40. make.left.right.top.equalTo(self.view);
  41. }];
  42.  
  43. //创建实例类tabbleViewDataSource
  44. //传入数据 这个可以根据实际情况灵活处理
  45. self.tabbleViewDataSource = [[HomeTableViewDataSource alloc]
  46. initWithItems:@[@"好好",@"学习",@"天天",@"向上"] ];
  47.  
  48. //设置tableView 两个代理
  49. self.tableView.dataSource = self.tabbleViewDataSource;
  50. self.tableView.delegate = self.tabbleViewDataSource;
  51.  
  52. //每个cell 行 点击触发 response
  53. TableViewCellBannerAction bannnerAction = ^(id sender) {
  54. NSLog(@"%@",sender);
  55. };
  56. TableViewCellDailyAction dailyAction = ^(id sender) {
  57. NSLog(@"%@",sender);
  58. };
  59. TableViewCellListAction listAction = ^(id sender) {
  60. NSLog(@"%@",sender);
  61. };
  62. self.tabbleViewDataSource.banneryAction = bannnerAction;
  63. self.tabbleViewDataSource.dailyAction = dailyAction;
  64. self.tabbleViewDataSource.listAction = listAction;
  65.  
  66. }
  67.  
  68. - (void)didReceiveMemoryWarning {
  69. [super didReceiveMemoryWarning];
  70. // Dispose of any resources that can be recreated.
  71. }
  72.  
  73. - (UITableView *)tableView
  74. {
  75. if (!_tableView) {
  76. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  77. _tableView.backgroundColor = [UIColor clearColor];
  78. //_tableView.dataSource = self;
  79. // _tableView.delegate = self;
  80. // _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  81.  
  82. [self.tableView registerClass:[HomeBannerCell class] forCellReuseIdentifier:kHomeBannerCellID];
  83. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
  84.  
  85. }
  86. return _tableView;
  87. }
  88.  
  89. @end

接下来是具体实施的HomeTableViewDataSource

  1. //
  2. // HomeTableViewDataSource.h
  3. // 链式DSL学习
  4. //
  5. // Created by HF on 17/3/8.
  6. // Copyright © 2017年 HF-Liqun. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. typedef void (^TableViewCellBannerAction)(id sender);
  12. typedef void (^TableViewCellDailyAction)(id sender);
  13. typedef void (^TableViewCellListAction)(id sender);
  14.  
  15. @interface HomeTableViewDataSource : NSObject <UITableViewDelegate, UITableViewDataSource>
  16. @property (nonatomic, copy) TableViewCellBannerAction banneryAction;
  17. @property (nonatomic, copy) TableViewCellDailyAction dailyAction;
  18. @property (nonatomic, copy) TableViewCellListAction listAction;
  19.  
  20. /**
  21. 传入数据源
  22.  
  23. @param anItems anItems
  24. @return anItems
  25. */
  26. - (id)initWithItems:(NSArray *)anItems;
  27.  
  28. @end
  29.  
  30. //
  31. // HomeTableViewDataSource.m
  32. // 链式DSL学习
  33. //
  34. // Created by HF on 17/3/8.
  35. // Copyright © 2017年 HF-Liqun. All rights reserved.
  36. //
  37.  
  38. #import "HomeTableViewDataSource.h"
  39. #import "HomeBannerCell.h"
  40.  
  41. typedef NS_ENUM(NSUInteger, HomeSectionType) {
  42. HomeSectionTypeBanner, //banner
  43. HomeSectionTypeDaily, //date
  44. HomeSectionTypeList //List
  45. };
  46.  
  47. @interface HomeTableViewDataSource ()
  48. {
  49. NSMutableArray *sectionInfo;
  50. }
  51. @property (nonatomic, strong) NSArray *items;
  52.  
  53. @end
  54.  
  55. @implementation HomeTableViewDataSource
  56.  
  57. - (id)init
  58. {
  59. return nil;
  60. }
  61.  
  62. - (id)initWithItems:(NSArray *)anItems
  63. {
  64. self = [super init];
  65. if (self) {
  66. sectionInfo = [NSMutableArray array];
  67. [sectionInfo addObject:@(HomeSectionTypeBanner)];
  68. [sectionInfo addObject:@(HomeSectionTypeDaily)];
  69. if (anItems.count > ) {
  70. [sectionInfo addObject:@(HomeSectionTypeList)]; //list 列表
  71. }
  72. self.items = anItems;
  73.  
  74. }
  75. return self;
  76. }
  77.  
  78. - (id)itemAtIndexPath:(NSIndexPath *)indexPath
  79. {
  80. return self.items[(NSUInteger) indexPath.row];
  81. }
  82.  
  83. #pragma mark UITableViewDataSource
  84.  
  85. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  86. {
  87. return sectionInfo.count;
  88. }
  89.  
  90. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  91. {
  92. if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) {
  93. return ;
  94. }
  95. if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) {
  96. return ;
  97. }
  98. if (HomeSectionTypeList == [sectionInfo[section] integerValue]) {
  99. return self.items.count;
  100. }
  101. return ;
  102. }
  103.  
  104. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. NSInteger section = indexPath.section;
  107. if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) {
  108. HomeBannerCell *cell = [tableView dequeueReusableCellWithIdentifier:kHomeBannerCellID forIndexPath:indexPath];
  109. return cell;
  110. }
  111. if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) {
  112. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"
  113. forIndexPath:indexPath];
  114. cell.textLabel.text = [NSString stringWithFormat:@"%@",[NSDate date]];
  115. cell.textLabel.font = [UIFont boldSystemFontOfSize:];
  116. cell.textLabel.textAlignment = NSTextAlignmentCenter;
  117. return cell;
  118. }
  119. if (HomeSectionTypeList == [sectionInfo[section] integerValue]) {
  120. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"
  121. forIndexPath:indexPath];
  122. id item = [self itemAtIndexPath:indexPath];
  123. cell.textLabel.text = [NSString stringWithFormat:@"%@",item];
  124.  
  125. return cell;
  126. }
  127. return [UITableViewCell new];
  128. }
  129.  
  130. #pragma mark UITableViewDelegate
  131.  
  132. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  133. {
  134. NSInteger section = indexPath.section;
  135. if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) {
  136. return [HomeBannerCell getCellHeight];
  137. }
  138. if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) {
  139. return ;
  140. }
  141. if (HomeSectionTypeList == [sectionInfo[section] integerValue]) {
  142. return ;
  143. }
  144. return ;
  145. }
  146.  
  147. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  148. {
  149. NSInteger section = indexPath.section;
  150. if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) {
  151. if (self.banneryAction) {
  152. self.banneryAction(@"点击了banner");
  153. }
  154. }
  155. if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) {
  156. if (self.dailyAction) {
  157. self.dailyAction(@"点击了日期");
  158. }
  159. }
  160. if (HomeSectionTypeList == [sectionInfo[section] integerValue]) {
  161. if (self.listAction) {
  162. id item = [self itemAtIndexPath:indexPath];
  163. self.listAction([NSString stringWithFormat:@"点击了list单元 %@",item]);
  164. }
  165. }
  166. }
  167.  
  168. @end 

参考:

1.https://objccn.io/issue-1-1/

iOS 给 ViewController 减负 之 UITableView的更多相关文章

  1. iOS全埋点解决方案-UITableView和UICollectionView点击事件

    前言 在 $AppClick 事件采集中,还有两个比较特殊的控件: UITableView •UICollectionView 这两个控件的点击事件,一般指的是点击 UITableViewCell 和 ...

  2. iOS开发UI篇—实现UItableview控件数据刷新

    iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...

  3. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)

    iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...

  4. iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建

    iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...

  5. IOS开发之表视图(UITableView)

    IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...

  6. iOS学习笔记之UITableViewController&UITableView

    iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...

  7. iOS学习笔记(4) — UITableView的 重用机制

    iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...

  8. iOS UI-表格控制器(UITableView)-基本使用

    tableView的常见属性 cell的常见属性 一.一般情况 #import "ViewController.h" @interface ViewController ()< ...

  9. iOS学习——ViewController(六)

    ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁,ViewController管理应用中的众多视图. iOS的SDK中提供很多原生ViewControlle ...

随机推荐

  1. C++注释规范

      1 源文件头部注释 列出:版权.作者.编写日期和描述. /************************************************* Copyright:bupt Auth ...

  2. hibernate的helloworld实现

    首先要新建一个 web project,然后新建一个lib的文件夹并导入相应的jar包 hibernate开发步骤: 1.创建hibernate配置文件 2.创建持久化类 3.创建对象关系映射文件 如 ...

  3. error: not found: value sc

    [问题] 解压spark的tar包后,执行bin/spark-shell,执行val lines=sc.textFile("README.md")时,抛错error: not fo ...

  4. Sphinx 安装与使用(1)-- 安装Coreseek

    Coreseek就是Sphinx的中文版 官方网站 http://www.coreseek.cn/ 一.安装 1.修改LANG 永久修改: vim /etc/locale.conf LANG=&quo ...

  5. 挑战:万能的slash! 判断js中“/”是正则、除号、注释?

    很久以前在其它地方就探讨和关注过这个问题,但都没有满意的解答. 看了zjfeihu 的帖子: <前端代码加亮插件(html,jss,css),支持即时加亮,运行代码>,再次提出这个比较经典 ...

  6. 算法之动态规划(最长递增子序列——LIS)

    最长递增子序列是动态规划中最经典的问题之一,我们从讨论这个问题开始,循序渐进的了解动态规划的相关知识要点. 在一个已知的序列 {a1, a 2,...an}中,取出若干数组成新的序列{ai1, ai ...

  7. [AC自己主动机+状压dp] hdu 2825 Wireless Password

    题意: 给n.m,k ,再给出m个单词 问长度为n的字符串.至少在m个单词中含有k个的组成方案有多少种. 思路: 因为m最大是10,所以能够採取状压的思想 首先建立trie图,在每一个单词的结束节点标 ...

  8. hadoop2.4.1伪分布模式部署

    hadoop2.4.1伪分布模式部署 (承接上一篇hadoop2.4.1-src的编译安装继续配置:http://www.cnblogs.com/wrencai/p/3897438.html) 感谢: ...

  9. day4笔记

    今日讲解内容:1,int数字:运算.1 ,2,3... # 数字类型:int #范围.用于运算, + - * / // %.... bit_lenth :十进制数字用二进制表示的最小位数 a=10 p ...

  10. 根据funID,personID获取最新规划包项目相关信息

    1.定义:根据funID,personID获取最新规划包项目相关信息(code projecttype(阶段) Pname(code+name) projectID) 项目表tbl_cfg_Proje ...