当前位置: 首页 > 编程日记 > 正文

通过分离dataSource 让我们的code具有更高的复用性.

转载自汪海的实验室

定义dataSource

dataSource.h

[objc] view plain copy
  1. typedef void (^TableViewCellConfigureBlock)(id cell, id item);  
  2.   
  3. @interface GroupNotificationDataSource : NSObject<UITableViewDataSource>  
  4.   
  5. - (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;  
  6.   
  7. - (id)itemAtIndexPath:(NSIndexPath *)indexPath;  
  8.   
  9. @end  

dataSource.m

[objc] view plain copy
  1. @implementation GroupNotificationDataSource  
  2. {  
  3.     NSArray     *_itemsArray;  
  4.     NSString    *_cellIdentifier;  
  5.     TableViewCellConfigureBlock _configureCellBlock;  
  6. }  
  7.   
  8. - (id)init{  
  9.     return nil;  
  10. }  
  11.   
  12. - (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock{  
  13.     self = [super init];  
  14.     if (self) {  
  15.         _itemsArray = anItems;  
  16.         _cellIdentifier = aCellIdentifier;  
  17.         _configureCellBlock = [aConfigureCellBlock copy];  
  18.     }  
  19.     return self;  
  20. }  
  21.   
  22. - (id)itemAtIndexPath:(NSIndexPath *)indexPath {  
  23.     return _itemsArray[(NSUInteger) indexPath.row];  
  24. }  
  25.   
  26. #pragma mark tableView DataSource  
  27. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  28.     return _itemsArray.count;  
  29. }  
  30.   
  31. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  32.     // NOTE: This method can return nil so you need to account for that in code   
  33.     GroupNotificationCell *cell=[tableView dequeueReusableCellWithIdentifier:_cellIdentifier];  
  34.     // NOTE: Add some code like this to create a new cell if there are none to reuse  
  35.     if (cell==nil) {  
  36.         cell=[[GroupNotificationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_cellIdentifier];  
  37.     }  
  38.     id item = [self itemAtIndexPath:indexPath];  
  39.     _configureCellBlock(cell, item);  
  40.     return cell;  
  41. }  

其中,需要注意tableView:cellForRowAtIndexPath:方法中对于cell的重用

重写cell的初始化

cell.h

[objc] view plain copy
  1. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;  

cell.m

[objc] view plain copy
  1. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{  
  2.     self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  3.     if (self) {  
  4.         [self configureUI];  
  5.     }  
  6.     return self;  
  7. }  

controller中设置tableview的dataSource

[objc] view plain copy
  1. _dataSource=[[GroupNotificationDataSource alloc]initWithItems:[NSArray arrayWithObjects:@"1",@"2", nil nil] cellIdentifier:CellIdentifier configureCellBlock:^(id cell, id item) {  
  2.         //  
  3.     }];  
  4.     _tableview.dataSource=_dataSource;  

需要注意的是_dataSource应该是成员变量,而非局部变量,否则会报错:message send to delloced object

相关文章:

复习心得 JAVA异常处理

java中的异常处理机制主要依赖于try&#xff0c;catch&#xff0c;finally&#xff0c;throw&#xff0c;throws五个关键字。其中&#xff0c; try关键字后紧跟一个花括号括起来的代码块&#xff08;花括号不可省略&#xff09;简称为try块。里面放置可能发生异常的代码。 catc…

Linux下Shell重定向

1. 标准输入&#xff0c;标准输出与标准错误输出 Linux下系统打开3个文件&#xff0c;标准输入&#xff0c;标准输出&#xff0c;标准错误输出。 标准输入:从键盘输入数据&#xff0c;即从键盘读入数据。 标准输出:把数据输出到终端上。 标准错误输出:把标准错误输出到终端上。…

sql avg函数使用格式_SQL AVG-SQL平均函数用语法示例解释

sql avg函数使用格式什么是SQL平均(AVG)函数&#xff1f; (What is the SQL Average (AVG) Function?) “Average” is an Aggregate (Group By) Function. It’s used to calculate the average of a numeric column from the set of rows returned by a SQL statement.“平均…

qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)

原博主博客地址&#xff1a;http://blog.csdn.net/qq21497936本文章博客地址&#xff1a;http://blog.csdn.net/qq21497936/article/details/78516201 qml学习笔记(二)&#xff1a;可视化元素基类Item详解&#xff08;上半场anchors等等&#xff09; 本学章节笔记主要详解Item元…

使用PowerShell登陆多台Windows,测试DCAgent方法

目标&#xff1a; 需要1台PC用域账户远程登陆10台PC&#xff0c;每台登陆后的PC执行发送敏感数据的操作后&#xff0c;再logoff。 在DCAgent服务器上&#xff0c;查看这10个用户每次登陆时&#xff0c;DCAgent是否能获取到登陆信息&#xff08;IP&#xff1a;User&#xff09; …

优雅地分离tableview回调

你是否遇到过这样的需求,在tableview中显示一列数据,点击某一个cell时&#xff0c;在此cell下显示相应的附加信息。如下图&#xff1a;你是不是觉得需求很容易实现&#xff0c;只要使用tableview的insertRowsAtIndexPaths:withRowAnimation:插入一个附加cell就可以了&#xff0…

next.js_Next.js手册

next.jsI wrote this tutorial to help you quickly learn Next.js and get familiar with how it works.我编写本教程是为了帮助您快速学习Next.js并熟悉其工作方式。 Its ideal for you if you have zero to little knowledge of Next.js, you have used React in the past,…

Redux 入门教程(一):基本用法

一年半前&#xff0c;我写了《React 入门实例教程》&#xff0c;介绍了 React 的基本用法。 React 只是 DOM 的一个抽象层&#xff0c;并不是 Web 应用的完整解决方案。有两个方面&#xff0c;它没涉及。代码结构组件之间的通信对于大型的复杂应用来说&#xff0c;这两方面恰恰…

Elasticsearch——Rest API中的常用用法

本篇翻译的是Elasticsearch官方文档中的一些技巧&#xff0c;是使用Elasticsearch必不可少的必备知识&#xff0c;并且适用于所有的Rest Api。 返回数据格式化 当在Rest请求后面添加?pretty时&#xff0c;结果会以Json格式化的方式显示。另外&#xff0c;如果添加?formatyaml…

Python几种主流框架

从GitHub中整理出的15个最受欢迎的Python开源框架。这些框架包括事件I/O&#xff0c;OLAP&#xff0c;Web开发&#xff0c;高性能网络通信&#xff0c;测试&#xff0c;爬虫等。 Django: Python Web应用开发框架Django 应该是最出名的Python框架&#xff0c;GAE甚至Erlang都有框…

Git Fetch vs Pull:Git Fetch和Git Pull命令之间有什么区别?

Git pull and fetch are two commands that are regularly used by Git users. Let’s see the difference between both commands.Git pull和fetch是Git用户经常使用的两个命令。 让我们看看两个命令之间的区别。 For the sake of context, it’s worth remembering that we’…

Redux 入门教程(二):中间件与异步操作

上一篇文章&#xff0c;我介绍了 Redux 的基本做法&#xff1a;用户发出 Action&#xff0c;Reducer 函数算出新的 State&#xff0c;View 重新渲染。但是&#xff0c;一个关键问题没有解决&#xff1a;异步操作怎么办&#xff1f;Action 发出以后&#xff0c;Reducer 立即算出…

day09_读写分离_组件介绍

mysql中间件研究&#xff08;Mysql-prxoy&#xff0c;Atlas&#xff0c;阿米巴&#xff0c;cobar&#xff0c;TDDL&#xff09;mysql-proxyMySQL Proxy就是这么一个中间层代理&#xff0c;简单的说&#xff0c;MySQL Proxy就是一个连接池&#xff0c;负责将前台应用的连接请求转…

Idea其他设置

一、生成javadoc Tools->Gerenate JavaDoc 1. 选择是整个项目还是模块还是单个文件 2. 文档输出路径 3. Locale 选择地区&#xff0c;这个决定了文档的语言&#xff0c;中文就是zh_CN 4. 传入JavaDoc的参数&#xff0c;一般这样写 -encoding UTF-8 -charset UTF-8 -windowti…

freecodecamp_常见技术支持问题– freeCodeCamp常见问题解答

freecodecamp问题&#xff1a;我刚刚登录我的帐户&#xff0c;但看不到过去的任何进展。 (Question: I just signed into my account and I dont see any of my past progress.) Answer: You have created a duplicate account. Sign out of your account and try signing in u…

Redux 入门教程(三):React-Redux 的用法

前两篇教程介绍了 Redux 的基本用法和异步操作&#xff0c;今天是最后一部分&#xff0c;介绍如何在 React 项目中使用 Redux。 为了方便使用&#xff0c;Redux 的作者封装了一个 React 专用的库 React-Redux&#xff0c;本文主要介绍它。 这个库是可以选用的。实际项目中&…

SDN第二次上机作业

1、安装floodlight 2、生成拓扑并连接控制器floodlight&#xff0c;利用控制器floodlight查看图形拓扑 3、利用字符界面下发流表&#xff0c;使得‘h1’和‘h2’ ping 不通 4、利用字符界面下发流表&#xff0c;通过测试‘h1’和‘h3’的联通性&#xff0c;来验证openflow的har…

[KIWI syslog]Install document

安全日志的标准是rfc5425 它介绍SSL-tunnel日志标准和相关要求的标准定义。此外&#xff0c;IANA分配TCP端口6514作为一个标准的端口安全日志。 安装说明&#xff1a; 1.安装日志服务器 官方下载地址&#xff1a;http://www.kiwisyslog.com/products/. 安装步骤&#xff1a; 转…

机器学习数据拆分_解释了关键的机器学习概念-数据集拆分和随机森林

机器学习数据拆分数据集分割 (Dataset Splitting) Splitting up into Training, Cross Validation, and Test sets are common best practices. This allows you to tune various parameters of the algorithm without making judgements that specifically conform to trainin…

Oracle 导表异常处理方案 (解决空表导出出错问题)

Select alter table ||table_name|| allocate extent; from user_tables where num_rows0 or num_rows is null 然后执行查询语句 再导出数据 一个语句搞定&#xff1a; declare stmt varchar2(200); begin for tb in (select table_name from user_tables where seg…

10个你必须知道的ios框架

你好&#xff0c;iOS 开发者们&#xff01;我的名字叫 Pawe?&#xff0c;我是一个独立 iOS 开发者&#xff0c;并且是 Enter Universe 的作者。 接近两年前我发布了iOS开源库&#xff0c;让你的开发坐上火箭吧。这是我在这里最棒的文章了&#xff08;根据 Medium 用户的反馈来…

生成N个不相等的随机数

近期项目中须要生成N个不相等的随机数。实现的时候。赶工期&#xff0c;又有项目中N非常小(0-100)直接谢了一个最直观的方法: public static List<Integer> randomSet(int num,int threshold){Random random new Random();if(num > threshold) return null;Set<In…

kafka streams_如何使用Kafka Streams实施更改数据捕获

kafka streamsChange Data Capture (CDC) involves observing the changes happening in a database and making them available in a form that can be exploited by other systems. 更改数据捕获 (CDC)涉及观察数据库中发生的更改&#xff0c;并将其以可被其他系统利用的形式…

iOS超全开源框架、项目和学习资料汇总(1)UI篇

上下拉刷新控件**1. ** MJRefresh --仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能。可以自定义上下拉刷新的文字说明。&#xff08;推荐&#xff09;**2. ** SVPullToRefresh --下拉刷新控件4500star&#xff0c;值得信赖**3. ** CBStoreHo…

day16 递归函数

一、递归 函数 为什么要有函数&#xff0c;提高代码的可读性&#xff0c;避免重复的代码&#xff0c;提高代码的复用性 在函数中能用return的不要print 1、递归的最大深度997 def foo(n):print(n)n1foo(n) foo(1) 递归的最大深度2、修改递归的最大深度 由此我们可以看出&#x…

设计模式之笔记--抽象工厂模式(Abstract Factory)

抽象工厂模式&#xff08;Abstract Factory&#xff09; 定义 抽象工厂模式&#xff08;Abstract Factory&#xff09;&#xff0c;提供一个创建一系列相关或相互依赖对象的接口&#xff0c;而无需指定它们具体的类。 类图 描述 多个抽象产品类&#xff0c;每个抽象产品类可以派…

用户体验改善案例_如何检测用户的设备,以便改善他们的用户体验

用户体验改善案例A few months ago I watched a great talk from the Chrome Dev Summit about performance in slow devices.几个月前&#xff0c;我观看了Chrome开发者峰会上有关慢速设备性能的精彩演讲。 It blew my mind all the work done by Facebook in identifying de…

【如何快速的开发一个完整的iOS直播app】(采集篇)

前言在看这篇之前&#xff0c;如果您还不了解直播原理&#xff0c;请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇)开发一款直播app&#xff0c;首先需要采集主播的视频和音频&#xff0c;然后传入流媒体服务器&#xff0c;本篇主要讲解如何采集主播的视频和音频&am…

easyui 报表合并单元格

前段时间工作中碰到有需求&#xff0c;要求数据按下图所示格式来显示&#xff0c;当时在园子里看到了一篇文章&#xff08;时间久了&#xff0c;想不起是哪一篇&#xff09;&#xff0c;研究了后做出了如下的DEMO&#xff0c;在此当作学习笔记&#xff0c;简单记录一下。 首先是…

HDU2594 KMP next数组的应用

这道题就是给你两个串s1, s2让你求出s1 s2的最长相同前缀和后缀&#xff0c; 我们直接将s1 s2连接到一起然后处理一下next数组即可&#xff0c; 注意答案应该是min(len(s1), len(s2) , next[len]), 代码如下&#xff1a; #include <cstdio> #include <cstring> #in…