最近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. centos7安装httpd和php

    centos7许多命令都变了,又要重新记了. centos7默认安装了httpd吧?记不清了,看一下: rpm -qa |grep httpd 没有的话,安装一下吧. yum -y install h ...

  2. 何为代理?jdk动态代理与cglib代理、spring Aop代理原理浅析

    原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...

  3. poj3304计算几何直线与线段关系

    Given n segments in the two dimensional space, write a program, which determines if there exists a l ...

  4. JDBC连接错误(Illegal mix of collations。。。)

    连接java和mysql时出现了这样的报错: java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) ...

  5. Mirantis MCP 1.0:OpenStack 和 Kubernetes 整合的第一步

    1.前言 Mirantis 公司在2014年9月14日宣布收购 TCPCloud,然后宣布在2017年第一季度会推出全新的私有云产品.从那时候开始,我就一直满怀期待.终于,今年4月19日,Mirant ...

  6. Saltstack自动化运维

    Saltstack三大功能 1,远程执行 2,配置管理(状态) 3,云管理 四种运行方式: Local         本地 Minion/Master C/S Syndic  代理模式 Salt S ...

  7. 2.let 和 const

    Babel 是一个广泛使用的 ES6 转码器,可以将 ES6 代码转为 ES5 代码,从而在现有环境执行. Babel 提供一个REPL在线编译器,可以在线将 ES6 代码转为 ES5 代码.转换后的 ...

  8. CSS预编译器:Sass(入门),更快的前端开发

    SASs是由美国注册会计师协会(AICPA)下属审计准则委员会(ASB)发布,是为了便于注册会计师执行和落实一般公认审计准则(GAAS). Sass 扩展了 CSS3,增加了规则.变量.混入.选择器. ...

  9. .Net程序员学用Oracle系列(25):触发器详解

    1.触发器理论 1.1.触发器的应用场景 1.2.触发器的类型 1.3.DML 触发器的触发顺序 2.触发器实战 2.1.创建触发器 2.1.1.创建 DML 触发器 2.1.2.创建 DDL 触发器 ...

  10. CI Weekly #19 | 关于软件开发模型的思考,以及最新 CI/CD 实践分享

    五月一来,夏天便悄然而至.flow.ci 也带来了几个新的变化,帮你进一步优化开发工作流.一起来看看这几个重点功能: 支持 iOS 项目 Xcode8.3 构建 iOSer 们重点来了,flow.ci ...