最近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. Vue+axios 实现http拦截及路由拦截

    现如今,每个前端对于Vue都不会陌生,Vue框架是如今最流行的前端框架之一,其势头直追react.最近我用vue做了一个项目,下面便是我从中取得的一点收获. 基于现在用vue+webpack搭建项目的 ...

  2. 视差滚动特效图片滑块-Sequence.js

    效果演示     插件下载

  3. 在Mvc中进行异步请求是出现(没有为该对象定义无参数的构造函数)

    解决办法就是给相应的类添加无参数的构造函数:

  4. Github开源:Sheng.RabbitMQ.CommandExecuter (RabbitMQ 的命令模式实现)

    [Github]:https://github.com/iccb1013/Sheng.RabbitMQ.CommandExecuter Sheng.RabbitMQ.CommandExecuter 是 ...

  5. (原创)性能测试中,Oracle服务器定位CPU使用率高的瓶颈(SQL)

    本篇博客记录一次性能测试过程中,定位对CPU使用率高的瓶颈问题,主要定位SQL为准 一.用SQL命令定位1.首先用TOP命令监控系统资源,如果是AIX系统,就用topas,进入TOP命令的滚动刷新数据 ...

  6. Facade模式——设计模式学习(转载)

    Facade模式 一 意图 为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 二 动机 将一个系统划分成为若干个子系统有利于降低系统的复 ...

  7. xml注释快捷键

    eclipse中编辑Java或C/C++文件时,注释的快捷键均为 "CTRL + / ",编辑xml文件时,该快捷键无效. eclipse XML 注释:CTRL + SHIFT ...

  8. WINFORM数据库操作,有点像安装里面的SQLITE

    程序设计要求 设计一个用户管理系统,对系统中的用户进行管理.假定,用户表中有下列字段:用户名,密码,电话和 email 等信息.要求,1)利用 SQL server 首先创建用户数据表:2)实现对用户 ...

  9. redis 清空缓存

    redis 清空缓存 Redis 命令: flushall --> 清空整个 Redis 服务器的数据(删除所有数据库的所有 key ) flushdb --> 清空当前数据库中的所有 k ...

  10. javaWeb学习总结(6)- 会话之cookie技术

    什么是会话? 在日常生活中,从拨通电话到挂断电话之间的一连串的你问我答的过程就是一个会话. 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为 ...