通过分离dataSource 让我们的code具有更高的复用性.
转载自汪海的实验室
一 定义dataSource
dataSource.h
- typedef void (^TableViewCellConfigureBlock)(id cell, id item);
- @interface GroupNotificationDataSource : NSObject<UITableViewDataSource>
- - (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;
- - (id)itemAtIndexPath:(NSIndexPath *)indexPath;
- @end
dataSource.m
- @implementation GroupNotificationDataSource
- {
- NSArray *_itemsArray;
- NSString *_cellIdentifier;
- TableViewCellConfigureBlock _configureCellBlock;
- }
- - (id)init{
- return nil;
- }
- - (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock{
- self = [super init];
- if (self) {
- _itemsArray = anItems;
- _cellIdentifier = aCellIdentifier;
- _configureCellBlock = [aConfigureCellBlock copy];
- }
- return self;
- }
- - (id)itemAtIndexPath:(NSIndexPath *)indexPath {
- return _itemsArray[(NSUInteger) indexPath.row];
- }
- #pragma mark tableView DataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return _itemsArray.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- // NOTE: This method can return nil so you need to account for that in code
- GroupNotificationCell *cell=[tableView dequeueReusableCellWithIdentifier:_cellIdentifier];
- // NOTE: Add some code like this to create a new cell if there are none to reuse
- if (cell==nil) {
- cell=[[GroupNotificationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_cellIdentifier];
- }
- id item = [self itemAtIndexPath:indexPath];
- _configureCellBlock(cell, item);
- return cell;
- }
其中,需要注意tableView:cellForRowAtIndexPath:方法中对于cell的重用
二 重写cell的初始化
cell.h
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
cell.m
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
- self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- [self configureUI];
- }
- return self;
- }
三 controller中设置tableview的dataSource
- _dataSource=[[GroupNotificationDataSource alloc]initWithItems:[NSArray arrayWithObjects:@"1",@"2", nil nil] cellIdentifier:CellIdentifier configureCellBlock:^(id cell, id item) {
- //
- }];
- _tableview.dataSource=_dataSource;
需要注意的是_dataSource应该是成员变量,而非局部变量,否则会报错:message send to delloced object
通过分离dataSource 让我们的code具有更高的复用性.相关推荐
- [翻译Joel On Software]Joel测试:12步写出更高质量代码/The Joel Test: 12 Steps to Better Code
Joel on Software The Joel Test: 12 Steps to Better Code Joel测试:12步写出更高质量代码 byJoel Spolsky Wednesday, ...
- Java前后端分离动态国际化(动态配置扩展性高)
介绍 主要是针对前后端分离场景国际化系统设计: 亮点: 1.动态国际化配置 2.可维护性 3.国际化数据池化(性能高) 4.后端数据内容动态国际化 5.提供前台动态国际化数据 6.后台异常国际化处理 ...
- 读书笔记-Office 365开发入门指南
作者博客 Office 365 开发概览系列 - 随笔分类 - 陈希章 - 博客园 https://www.cnblogs.com/chenxizhang/category/967796.html ...
- 微信小程序云开发之网易云音乐
这是跟着慕课网谢成老师敲得代码 注意 知识点 疑惑 思路 妙 优化 一个要注意的地方:如果获取歌词的时候报错(才发现解决办法的图片没传上来 大概就是改变一下请求头数据) 第一章课程介绍 云开发(音乐播 ...
- 摘抄Django项目(一)
果生鲜项目介绍 1.商业模式说明 2.开发流程介绍 3.项目需求分析 4.项目架构分析 5.数据库设计 6.模型类说明 7.创建dailyfresh项目 8.展示注册页面 9.视图函数的get和pos ...
- Django 框架 要点
Python Web 框架要点 1. Web应用程序处理流程 2. Web程序框架的意义 用于搭建Web应用程序 免去不同Web应用相同代码部分的重复编写,只需关心Web应用核心的业务逻辑实现 3. ...
- 设计系统简史——过去,现在与可能的未来
本文共6749个字,预计阅读时间15分钟. 我第一次听说"设计系统"这个概念是在2016年初,契机是在某设计师招聘要求的栏目中看到了"熟悉至少一种设计系统,例如 HIG ...
- Java面向对象之全集
面向对象 什么是面向对象? 面向对象(Object-Oriented,简称OO)是一种编程方法,它将数据和对数据的操作封装在一个对象中,使得程序更易理解.修改和扩展.面向对象思想认为,计算机程序应该被 ...
- 从Code Review 谈如何做技术
本文转载自 www.coolshell.cn (这篇文章缘由我的微博,我想多说一些,有些杂乱,想到哪写到哪) 这两天,在微博上表达了一下Code Review的重要性.因为翻看了阿里内部的Review ...
最新文章
- 牵引力人事主管告诉你:如何面试Web前端开发工程师
- ocx js php,JS实现OCX控件的事件响应示例_javascript技巧
- cms系统与dreamweaver建站
- Ado.net连接池 sp_reset_connection 概念
- php中时差怎么改,我的PHP时差功能可以改进吗?
- 现代软件工程 其实还是人的问题
- 网络切片技术缺点_一文读懂网络切片 - 技术综合版块 - 通信人家园 - Powered by C114...
- 微信或正测试“发送语音过程”转文字功能 部分安卓用户可用
- 线性基的一些基础模版
- jQuery基础部分
- 为什么不发展三进制计算机,三进制计算机
- 接着说tableview的tableheadview
- netty源码分析及优点
- 绿能宝承载的不单是绿能宝的梦想
- xilinx官网下载vivado速度慢的解决方法(适用于所有版本)
- Word在方框中插入对勾和×
- .c_str()函数解析
- php 清除js,php,js清除cookie
- 《安富莱嵌入式周报》第298期:迷你火星探测器,开源单片机3D实时渲染库, 开源USB工业相机,VS2022开始支持MarkDown,PC-lint 2.0发布
- 985毕业,半路出家28岁进军Java,坚持了三年现如今年薪36W