iOS 给 ViewController 减负 之 UITableView
今天看了一些博客文章分享了如何给ViewController 瘦身的问题, 其中一个就是tableView.
的确,随着产品迭代,VC里面可能越来越臃肿,有时候真的需要好好进行一次瘦身.可能是参考的博客中讲解更侧重于方法的复用,其实这个真的是很灵活的问题,有时候tableView list 都是同一规格就比较适合可复用的方法去实现,有时候就是需要单独自定义的,一个tableView 可能有不止两到三个cell 类型的情况. 所以针对后者.我也仿照着写了一个 把 UITableViewDataSource UITableViewDelegate 都提取出来单独做一个对象做处理.
需求:一个tableView 里面目前元素是 一行banner 一行时间 若干行list 元素组成
效果:

SectionDemo 如下:
//
// ViewController减负.h
// 链式DSL学习
//
// Created by HF on 17/3/8.
// Copyright © 2017年 HF-Liqun. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController__ : UIViewController @property (nonatomic, strong) UITableView *tableView; @end
//
// ViewController减负.m
// 链式DSL学习
//
// Created by HF on 17/3/8.
// Copyright © 2017年 HF-Liqun. All rights reserved.
// #import "ViewController减负.h"
#import "HomeBannerCell.h"
#import "HomeTableViewDataSource.h" @interface ViewController__ () @property (nonatomic, strong) HomeTableViewDataSource *tabbleViewDataSource;
@end @implementation ViewController__ - (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.view);
make.left.right.top.equalTo(self.view);
}]; //创建实例类tabbleViewDataSource
//传入数据 这个可以根据实际情况灵活处理
self.tabbleViewDataSource = [[HomeTableViewDataSource alloc]
initWithItems:@[@"好好",@"学习",@"天天",@"向上"] ]; //设置tableView 两个代理
self.tableView.dataSource = self.tabbleViewDataSource;
self.tableView.delegate = self.tabbleViewDataSource; //每个cell 行 点击触发 response
TableViewCellBannerAction bannnerAction = ^(id sender) {
NSLog(@"%@",sender);
};
TableViewCellDailyAction dailyAction = ^(id sender) {
NSLog(@"%@",sender);
};
TableViewCellListAction listAction = ^(id sender) {
NSLog(@"%@",sender);
};
self.tabbleViewDataSource.banneryAction = bannnerAction;
self.tabbleViewDataSource.dailyAction = dailyAction;
self.tabbleViewDataSource.listAction = listAction; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor clearColor];
//_tableView.dataSource = self;
// _tableView.delegate = self;
// _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.tableView registerClass:[HomeBannerCell class] forCellReuseIdentifier:kHomeBannerCellID];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"]; }
return _tableView;
} @end
接下来是具体实施的HomeTableViewDataSource 类
//
// HomeTableViewDataSource.h
// 链式DSL学习
//
// Created by HF on 17/3/8.
// Copyright © 2017年 HF-Liqun. All rights reserved.
// #import <Foundation/Foundation.h> typedef void (^TableViewCellBannerAction)(id sender);
typedef void (^TableViewCellDailyAction)(id sender);
typedef void (^TableViewCellListAction)(id sender); @interface HomeTableViewDataSource : NSObject <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, copy) TableViewCellBannerAction banneryAction;
@property (nonatomic, copy) TableViewCellDailyAction dailyAction;
@property (nonatomic, copy) TableViewCellListAction listAction; /**
传入数据源 @param anItems anItems
@return anItems
*/
- (id)initWithItems:(NSArray *)anItems; @end //
// HomeTableViewDataSource.m
// 链式DSL学习
//
// Created by HF on 17/3/8.
// Copyright © 2017年 HF-Liqun. All rights reserved.
// #import "HomeTableViewDataSource.h"
#import "HomeBannerCell.h" typedef NS_ENUM(NSUInteger, HomeSectionType) {
HomeSectionTypeBanner, //banner
HomeSectionTypeDaily, //date
HomeSectionTypeList //List
}; @interface HomeTableViewDataSource ()
{
NSMutableArray *sectionInfo;
}
@property (nonatomic, strong) NSArray *items; @end @implementation HomeTableViewDataSource - (id)init
{
return nil;
} - (id)initWithItems:(NSArray *)anItems
{
self = [super init];
if (self) {
sectionInfo = [NSMutableArray array];
[sectionInfo addObject:@(HomeSectionTypeBanner)];
[sectionInfo addObject:@(HomeSectionTypeDaily)];
if (anItems.count > ) {
[sectionInfo addObject:@(HomeSectionTypeList)]; //list 列表
}
self.items = anItems; }
return self;
} - (id)itemAtIndexPath:(NSIndexPath *)indexPath
{
return self.items[(NSUInteger) indexPath.row];
} #pragma mark UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return sectionInfo.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) {
return ;
}
if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) {
return ;
}
if (HomeSectionTypeList == [sectionInfo[section] integerValue]) {
return self.items.count;
}
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger section = indexPath.section;
if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) {
HomeBannerCell *cell = [tableView dequeueReusableCellWithIdentifier:kHomeBannerCellID forIndexPath:indexPath];
return cell;
}
if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"
forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@",[NSDate date]];
cell.textLabel.font = [UIFont boldSystemFontOfSize:];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
return cell;
}
if (HomeSectionTypeList == [sectionInfo[section] integerValue]) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"
forIndexPath:indexPath];
id item = [self itemAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@",item]; return cell;
}
return [UITableViewCell new];
} #pragma mark UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger section = indexPath.section;
if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) {
return [HomeBannerCell getCellHeight];
}
if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) {
return ;
}
if (HomeSectionTypeList == [sectionInfo[section] integerValue]) {
return ;
}
return ;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger section = indexPath.section;
if (HomeSectionTypeBanner == [sectionInfo[section] integerValue]) {
if (self.banneryAction) {
self.banneryAction(@"点击了banner");
}
}
if (HomeSectionTypeDaily == [sectionInfo[section] integerValue]) {
if (self.dailyAction) {
self.dailyAction(@"点击了日期");
}
}
if (HomeSectionTypeList == [sectionInfo[section] integerValue]) {
if (self.listAction) {
id item = [self itemAtIndexPath:indexPath];
self.listAction([NSString stringWithFormat:@"点击了list单元 %@",item]);
}
}
} @end
参考:
1.https://objccn.io/issue-1-1/
iOS 给 ViewController 减负 之 UITableView的更多相关文章
- iOS全埋点解决方案-UITableView和UICollectionView点击事件
前言 在 $AppClick 事件采集中,还有两个比较特殊的控件: UITableView •UICollectionView 这两个控件的点击事件,一般指的是点击 UITableViewCell 和 ...
- iOS开发UI篇—实现UItableview控件数据刷新
iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...
- IOS开发之表视图(UITableView)
IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...
- iOS学习笔记之UITableViewController&UITableView
iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...
- iOS学习笔记(4) — UITableView的 重用机制
iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...
- iOS UI-表格控制器(UITableView)-基本使用
tableView的常见属性 cell的常见属性 一.一般情况 #import "ViewController.h" @interface ViewController ()< ...
- iOS学习——ViewController(六)
ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁,ViewController管理应用中的众多视图. iOS的SDK中提供很多原生ViewControlle ...
随机推荐
- C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 2
Chapter 2.1 1. 数据类型决定了程序中数据和操作的意义. 2. C++定义了一套基本数据类型,其中包括算术类型和一个名为void的特殊类型.算术类型包含了字符.整型.布尔值以及浮点数.vo ...
- 如何让git小乌龟工具TortoiseGit记住你的账号密码
在使用小乌龟的过程中,发下每次push或者pull都要重复输入账号密码,非常麻烦. 如果能记住账号密码就好了,这样就省去了时间. 怎么设置记住密码 在[系统盘]:\Users[你的用户名]下面,有一个 ...
- CentOS上使用Squid+Stunnel搭建代理服务器教程
这篇文章主要介绍了CentOS上使用Squid+Stunnel搭建代理服务器教程,同时文中也介绍了用户认证的方法,适合于多用户共同使用代理,这种功能在国内用还是比较exciting的~需要的朋友可以参 ...
- void bind(String sName,Object object);――绑定:把名称同对象关联的过程
void bind(String sName,Object object);――绑定:把名称同对象关联的过程 void rebind(String sName,Object object);――重新绑 ...
- linux c 常常混淆的概念
指针函数 and 函数指针 指针函数是指带指针的函数,即本质是一个函数.函数都有返回类型(假设不返回值,则为无值型),仅仅只是指针函数返回类型是某一类型的指针. 定义格式 类型名 *函数名(函数參数列 ...
- 框架应用:Mybatis - 开发详述
ORM框架 在实际开发中,工程中本质的任务是从数据库中获取数据,然后对数据进行操作,又或者写入数据.开发时语言是大多是面向对象的工程语言,这个时候就必须进行工程语言和数据库连接语言的转换,也就是所谓的 ...
- 在MathType中输入罗马数字的方法
MathType作为数学公式编辑器的编辑功能非常的强大,其中包含了许许多多各种各样的数学符号,甚至标记符号也很全面.编辑公式时有时为了让公式看起来会更有条理,会进行一定的序号设置,当然也可以对公式进行 ...
- Jmeter与LoadRunner 测试Java项目的坑
32位的JDK,Jmeter.bat 最大内存只能配置1G,测不了大并发,所以用Jmeter测试时一定要改成64位的Jmeter用LR测试java程序的时候必须用32位的JDK 环境变量 在path的 ...
- bootstrap获取总条目数
$('#table').on('load-success.bs.table', function () {alert($('#table').bootstrapTable('getOptions'). ...
- B - The Suspects(并查集)
B - The Suspects Time Limit:1000MS Memory Limit:20000KB 64bit IO Format:%lld & %llu Desc ...