iOS 从UITableViewController中分离数据源
之前看objc.io #1 Light View Controllers看到一个非常不错的技巧:从UITableViewController中分离数据源,这样能够减小UITableViewController的规模。同一时候也能让程序有一个比較好的架构。
因为UITableViewController是iOS中使用得最频繁的一个视图控制器,所以这里做下笔记,记录下这个技巧。
首先是故事板(当然也能够用代码 + XIB的组合):
新建一个Cell类,连接故事板中的Outlets,代码例如以下:
#import <UIKit/UIKit.h> @interface Cell : UITableViewCell - (void)configureForData:(NSString *)data; @end
#import "Cell.h"
@interface Cell ()
@property (weak, nonatomic) IBOutlet UILabel *dataTitleLabel;
@property (weak, nonatomic) IBOutlet UIButton *dataDetailLabel;
@end
@implementation Cell
- (void)configureForData:(NSString *)data {
self.dataTitleLabel.text = data;
[self.dataDetailLabel setTitle:@"1" forState:UIControlStateNormal];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
Cell类中的configureForData方法用于配置Cell中UI的内容。
回到TableViewController类。代码例如以下:
#import "TableViewController.h"
#import "DataSource.h"
#import "Cell.h" @interface TableViewController () @property (strong, nonatomic) NSArray *array;
@property (strong, nonatomic) DataSource *dataSource; @end @implementation TableViewController - (void)viewDidLoad {
[super viewDidLoad]; self.array = @[@"1", @"2", @"3", @"1", @"2", @"3", @"1", @"2", @"3"]; [self setupTableView];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /* 设置表格的数据源。并registerNib */
- (void)setupTableView { TableViewCellConfigureBlock configureCell = ^(Cell *cell, NSString *str) {
[cell configureForData:str];
};
self.dataSource = [[DataSource alloc] initWithItems:_array
cellIdentifier:@"Cell"
configureCellBlock:configureCell];
self.tableView.dataSource = self.dataSource;
} #pragma mark - UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100.0;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%@", self.array[indexPath.row]);
} @end
当中setupTableView方法将表格的UITableViewDataSource “外包”给TableDataSource类实现。
本类实现UITableViewDelegate。包含点击表格中的某一行的行为。cell的高度等。
最后看看承担表格数据源责任的TableViewDataSource类:
#import <Foundation/Foundation.h> typedef void (^TableViewCellConfigureBlock)(id cell, id item); @interface DataSource : NSObject <UITableViewDataSource> - (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock; - (id)itemAtIndexPath:(NSIndexPath *)indexPath; @end
首先该类必须遵守UITableViewDataSource托付,然后定义一个配置Cell的Block类型。
该类的实现代码例如以下:
#import "DataSource.h" @interface DataSource () @property (nonatomic, strong) NSArray *items;
@property (nonatomic, copy) NSString *cellIdentifier;
@property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock; @end @implementation DataSource #pragma mark - Initialization - (id)init {
// 仅仅能通过initWithItems:cellIdentifier:configureCellBlock:方法初始化
return nil;
} - (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
{
self = [super init]; if (self) {
self.items = anItems;
self.cellIdentifier = aCellIdentifier;
self.configureCellBlock = [aConfigureCellBlock copy];
} return self;
} - (id)itemAtIndexPath:(NSIndexPath *)indexPath {
return self.items[(NSUInteger) indexPath.row];
} #pragma mark UITableViewDataSource /* Required methods */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.items.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier
forIndexPath:indexPath];
id item = [self itemAtIndexPath:indexPath]; /**
之所以把configureCellBlock作为一个属性,是为了该类能够被复用
仅仅要TableViewController定制了相应的代码块并作为參数传入就能够了 复用的关键:不要被详细的实现代码入侵。仅仅须要调用接口和给出接口就能够了
*/
self.configureCellBlock(cell, item);
return cell;
} @end
说下cellForRowAtIndexPath方法中的self.configureCellBlock(cell, item);
这句代码的作用无疑是配置Cell中的内容,一般由用户自己定义的Cell类自行实现,这里没有牵涉不论什么实现细节,从而保证TableViewDataSource类能够非常好地被复用。
执行结果:
顺便传了个Demo上来,有兴趣的能够下载看看。
iOS 从UITableViewController中分离数据源的更多相关文章
- iOS中UITableView数据源刷新了,但tableview当中的cell没有刷新
你会不会遇到通过断点查看数据源模型的确刷新了,但是tableview没有刷新的情况,我遇到了,并通过下面的方法解决了,供大家参考! 在tableview中的数据源代理方法 p.p1 { margin: ...
- iOS开发 Xcode8中遇到的问题及改动
iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功能,或者苹果健康 ...
- iOS开发——基础篇——iOS开发 Xcode8中遇到的问题及改动
iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功能,或者苹果健康 ...
- 在iOS应用程序中使用Frida绕过越狱检测
阿里聚安全在之前的三篇博客中介绍了利用Frida攻击Android应用程序,整个过程仿佛让开发者开启上帝视角,在本篇博客中,我们将会介绍在iOS应用程序中使用Frida绕过越狱检测.即使 ...
- iOS学习——tableview中带编辑功能的cell键盘弹出遮挡和收起问题解决
最近在项目中经常用到UITableView中的cell中带有UITextField或UITextView的情况,然后在这种场景下,当我们点击屏幕较下方的cell进行编辑时,这时候键盘弹出来会出现遮挡待 ...
- iOS UITableViewCell UITableVIewController 纯代码开发
iOS UITableViewCell UITableVIewController 纯代码开发 <原创> .纯代码 自定义UITableViewCell 直接上代码 ////// #imp ...
- IOS Swift UITableViewcontroller实现点击空白处隐藏键盘
在ios开发中,为了方便,我们经常使用UITableViewcontroller,比如搜索界面为了方便可能更多的使用UITableViewcontroller,那么问题就来了,当我点击搜索框的时候会弹 ...
- iOS: 在UIViewController 中添加Static UITableView
如果你直接在 UIViewController 中加入一个 UITableView 并将其 Content 属性设置为 Static Cells,此时 Xcode 会报错: Static table ...
- iOS 解决LaunchScreen中图片加载黑屏问题
iOS 解决LaunchScreen中图片加载黑屏问题 原文: http://blog.csdn.net/chengkaizone/article/details/50478045 iOS 解决Lau ...
随机推荐
- manifest
manifest是一种软件,属于AndroidManifest.xml文件,在简单的Android系统的应用中提出了重要的信息,它可以运行任何应用程序的代码. 每个安卓应用程序必须有一个Android ...
- vue中如何将时间对象转换成字符串
借鉴element-admin中封装好的方法 import { parseTime } from '@/utils'// 在utils目录下的index.js文件中,方法如下 /** * Parse ...
- 关于python的整形(int)自动转长整形(long)的问题
有时需要访问某个接口,其中传入的整形参数可能比较长就会变成long,这时如果用str()的话‘L’就会被转化到字符串中,导致接口不能识别: 这种情况下应该优先使用json来转译,可以完美保持翻译pyt ...
- BZOJ 2813: 奇妙的Fibonacci
2813: 奇妙的Fibonacci Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 497 Solved: 134[Submit][Status][ ...
- aspx生成静态页面html 例子
原文发布时间为:2009-07-26 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- HDU5006 Resistance(高斯消元)
给你一个复杂的网路图,然后告诉你s,t,求s,t的等效电阻.方法是设s的电势为1,t的电势为0.然后对于其它的每个点x,满足的是sigma(ux-uy)/R(x,y)(即对每个与x相连的节点y,电势差 ...
- 牛客网 牛客小白月赛1 J.おみやげをまらいました
J.おみやげをまらいました 链接:https://www.nowcoder.com/acm/contest/85/J来源:牛客网 随便写写. 代码: 1 #include<ios ...
- 关于TP中U方法,在wamp中是绝对路径,在nginx中是相对路径?(坑)
这个问题已多次遇到,关于tp 框架 使用U 方法跳转, 在Nginx 服务器上可能会遇到路由跳转不过去前面带点(如:./xx) 解决这个问题,可以在tp的入口文件 index.php 里定义个常量 d ...
- win7下用PyInstaller把Python代码打包成exe文件
2013-11-05 22:02:14| 1.安装 使用PyInstaller需要安装PyWin32. 下载与Python对应的PyInstaller版本,解压后就算安装好了. 例如,安装了PyI ...
- 【Kafka】《Kafka权威指南》——写数据
不管是把 Kafka 作为消息队列.消息.总线还是数据存储平台来使用 ,总是需要有一个可以往 Kafka 写入数据的生产者和一个可以从 Kafka读取数据的消费者,或者一个兼具两种角 色的应用程序. ...