最近4年的时间,我已经做了5个App完全独立开发, 工作经历5个App, 维护了两个App.

在这期间用的最多的是UITableView, 因此也有许多感觉可以封装的.

现在就是我封装的.

RXCell 继承于UITableViewCell

// 所以TableViewCell的基类
@interface RXCell : UITableViewCell
// 用data更新cell数据
- (void)setData:(id)data;
// 从xib中获取cell,记得在xib设置identify
+ (id)cell_xib;
// 用initWithStyle:reuseIdentifier:方式获取cell
+ (instancetype)cell;
// cell 的高度,需要子类去实现
+ (CGFloat)cellHeight;
// 默认的是Class的字符串,子类不需要重写
+ (NSString *)identifier;
@end @implementation RXCell
- (void)setData:(id)data
{
// Do Nothing
}+ (id)cell_xib
{
NSArray *nibView = [[NSBundle mainBundle] loadNibNamed:[self identifier] owner:nil options:nil];
return nibView[];
}
+ (instancetype)cell
{
Class cls = [self class];
id cell = [[cls alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self identifier]];
return cell;
}
+ (CGFloat)cellHeight
{
return ;
}
+ (NSString *)identifier
{
return NSStringFromClass([self class]);
}
@end

RXFunctionItem

@interface RXFunctionItem : NSObject
@property (nonatomic, copy) NSString *iconName; // 图片名称
@property (nonatomic, copy) NSString *title; // 名称
@property (nonatomic, assign) SEL action; // action
@property (nonatomic, assign) int type; // type
@property (nonatomic, strong) id object; // 扩展数据
- (id)initWithIconName:(NSString *)iconName title:(NSString *)title action:(SEL)action type:(int)type;
- (id)initWithIconName:(NSString *)iconName title:(NSString *)title action:(SEL)action type:(int)type object:(id)object;
@end

在自定义的TestCell需要添加代码:

@interface TestCell ()
@property (weak, nonatomic) IBOutlet UILabel *lblTitle;
@end
@implementation TestCell
- (void)setData:(id)data
{
if ([data isKindOfClass:[RXFunctionItem class]]) {
RXFunctionItem *tmp = data;
self.lblTitle.text = tmp.title;
}
}
- (void)awakeFromNib {
// Initialization code
self.lblTitle.backgroundColor = [UIColor redColor];
}
+ (CGFloat)cellHeight
{
return ;
}
@end

在VC中使用:

#pragma mark - UITableViewDataSource
#pragma mark Required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.functionItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id item = self.functionItems[indexPath.row];
NSString *identify = [TestCell identifier];
TestCell *cell = (TestCell *)[tableView dequeueReusableCellWithIdentifier:identify];
if (cell == nil) {
cell = [TestCell cell_xib];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell setData:item];
return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [TestCell cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RXFunctionItem *item = self.functionItems[indexPath.row];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (item.action != nil) {
[self performSelector:item.action withObject:item afterDelay:];
}
}
#pragma mark - Action
- (void)cell0Action:(id)sender
{
NSLog(@"cell0Action:%@", sender);
}
- (void)cell1Action:(id)sender
{
NSLog(@"cell1Action:%@", sender);
}
- (void)cell2Action:(id)sender
{
NSLog(@"cell2Action:%@", sender);
}
#pragma mark - View Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
RXFunctionItem *item0 = [[RXFunctionItem alloc] initWithIconName:@"" title:@"第1行" action:@selector(cell0Action:) type: object:nil];
RXFunctionItem *item1 = [[RXFunctionItem alloc] initWithIconName:@"" title:@"第2行" action:@selector(cell1Action:) type: object:nil];
RXFunctionItem *item2 = [[RXFunctionItem alloc] initWithIconName:@"" title:@"第3行" action:@selector(cell2Action:) type: object:nil];
self.functionItems = @[item0, item1, item2];
[self.tableView reloadData];
}

思路是:

VC 用RXFunctionItem准备好数据, 然后给UITableViewDataSource和UITableViewDelegate使用,

在cellForRowAtIndexPath中把数据取出来, 然后Cell取刷新数据.

参考: https://github.com/xzjxylophone/RXTableViewItem

封装TableView有可能用到的数据结构和UITableViewCell的一个继承类的更多相关文章

  1. 共用tableview一个继承类里面有

    里面的复用cell会不会混在一起呢?

  2. 使用libzplay库封装一个音频类

    装载请说明原地址,谢谢~~      前两天我已经封装好一个duilib中使用的webkit内核的浏览器控件和一个基于vlc的用于播放视频的视频控件,这两个控件可以分别用在放酷狗播放器的乐库功能和MV ...

  3. 个人封装的一个Camera类

    好久不写博客了,代码写了不少,但大多数都是拿来主义,要不是网上,要不就是自己曾经的代码拼装. 新工作是搞Android开发的,近期任务要求我封装一个Carmera类,自己也认为还是封装以后方便使用,弄 ...

  4. 1.使用C++封装一个链表类LinkList

     使用C++封装一个链表类LinkList.写出相应一个测试用例 链表需要提供 添加 修改删除 除重 合并 排序创建 销毁等接口. 不能调用库函数或者使用STL等类库 题目延伸********** ...

  5. SpringMVC 中,当前台传入多个参数时,可将参数封装成一个bean类

    在实际业务场景中,当前台通过 url 向后台传送多个参数时,可以将参数封装成一个bean类,在bean类中对各个参数进行非空,默认值等的设置. 前台 url ,想后台传送两个参数,userName 和 ...

  6. Swift - 简单封装一个工具类模板

    创建模板类(封装一个类) 例1:新建一个名字叫做 Product 的类 Product.swift File 的内容 class Product { var name: String var desc ...

  7. js原生设计模式——2面向对象编程之继承—原型继承(类式继承的封装)

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  8. 结合数据结构来看看Java的String类

    数据结构中定义字符串是由零个或多个字符组成的有限序列,有限,指出字符串的长度是一个有限的数值:所谓的序列,说明串的相邻字符之间具有前驱和后继的关系.字符串一般记为s="a1a2...an&q ...

  9. RecyclerView.Adapter封装,最简单实用的BaseRecyclerViewAdapter;只需重写一个方法,设置数据链式调用;

    之前对ListView的BaseAdapter进行过封装,只需重写一个getView方法: 现在慢慢的RecyclerView成为主流,下面是RecyclerView.Adapter的封装: Base ...

随机推荐

  1. Linux防火墙配置—访问外网WEB

    一.实验目标 1.本次实验在"Linux基础网络搭建实验"的基础上,在外网虚拟机上搭建WEB服务,并分别配置外网和网关的防火墙规则,使内网能够访问WEB服务 2.Linux基础网络 ...

  2. 漏洞预警 | Apache Struts2 曝任意代码执行漏洞 (S2-045)

    近日,Apache官方发布Apache Struts 2.3.5–2.3.31版本及2.5–2.5.10版本存在远程代码执行漏洞(CNNVD-201703-152 ,CVE-2017-5638)的紧急 ...

  3. PHP获取Post的原始数据方法小结(POST无变量名)

    From : http://blog.csdn.net/hotdigger/article/details/6456240   一般我们都用$_POST或$_REQUEST两个预定义变量来接收POST ...

  4. Vue2.x中的父组件数据传递至子组件

    父组件结构 template <template> <div> <v-girl-group :girls="aGirls"></v-gir ...

  5. 【2017-05-02】winform弹出警告框是否进行增删改操作、记事本制作、对话框控件和输出输入流

    一.winform弹出警告框是否进行增删改操作 第一个参数是弹出窗体显示的内容,第二个参数是标题,第三个参数是该弹窗包含确定和取消按钮. 返回的是一个枚举类接收一下. 再进行判断,如果点的是确定按钮, ...

  6. PHP学习笔记-3

    PHP 数据类型: 字符串.整数.浮点数.逻辑.数组.对象.NULL. JavaScript数据类型: 字符串.数字.布尔.数组.对象.Null.Undefined. 从上面可以看出来,数据类型都是7 ...

  7. Java的CLASSPATH

    在JDK安装好后,要设置两个变量Path和Classpath,Path是操作系统要求的,这里不谈了,而classpath是Java虚拟机要求的这里做一个详细的解释. 一.classpath的作用 == ...

  8. JavaScript 复制对象

    在JavaScript这门语言中,数据类型分为两大类:基本数据类型和复杂数据类型.基本数据类型包括Number.Boolean.String.Null.String.Symbol(ES6 新增),而复 ...

  9. 封装GCD以及介绍如何使用

    研究GCD有一段时间,翻译了多篇文章,找了很多的资料,看了很多官方文档,看起来很难,实际上很简单,本人一一进行讲解怎么使用. 支持ARC以及非ARC,无论在ARC环境还是在非ARC环境,都需要调用di ...

  10. poj2253 Frogger Dijkstra变形

    题目链接:http://poj.org/problem?id=2253 就是求所有路径的最大边权值的最小值 处理时每次找出距离当前的已选的节点的最短距离,然后更新每个未选节点的值 代码: #inclu ...