封装TableView有可能用到的数据结构和UITableViewCell的一个继承类
最近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的一个继承类的更多相关文章
- 共用tableview一个继承类里面有
里面的复用cell会不会混在一起呢?
- 使用libzplay库封装一个音频类
装载请说明原地址,谢谢~~ 前两天我已经封装好一个duilib中使用的webkit内核的浏览器控件和一个基于vlc的用于播放视频的视频控件,这两个控件可以分别用在放酷狗播放器的乐库功能和MV ...
- 个人封装的一个Camera类
好久不写博客了,代码写了不少,但大多数都是拿来主义,要不是网上,要不就是自己曾经的代码拼装. 新工作是搞Android开发的,近期任务要求我封装一个Carmera类,自己也认为还是封装以后方便使用,弄 ...
- 1.使用C++封装一个链表类LinkList
使用C++封装一个链表类LinkList.写出相应一个测试用例 链表需要提供 添加 修改删除 除重 合并 排序创建 销毁等接口. 不能调用库函数或者使用STL等类库 题目延伸********** ...
- SpringMVC 中,当前台传入多个参数时,可将参数封装成一个bean类
在实际业务场景中,当前台通过 url 向后台传送多个参数时,可以将参数封装成一个bean类,在bean类中对各个参数进行非空,默认值等的设置. 前台 url ,想后台传送两个参数,userName 和 ...
- Swift - 简单封装一个工具类模板
创建模板类(封装一个类) 例1:新建一个名字叫做 Product 的类 Product.swift File 的内容 class Product { var name: String var desc ...
- js原生设计模式——2面向对象编程之继承—原型继承(类式继承的封装)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 结合数据结构来看看Java的String类
数据结构中定义字符串是由零个或多个字符组成的有限序列,有限,指出字符串的长度是一个有限的数值:所谓的序列,说明串的相邻字符之间具有前驱和后继的关系.字符串一般记为s="a1a2...an&q ...
- RecyclerView.Adapter封装,最简单实用的BaseRecyclerViewAdapter;只需重写一个方法,设置数据链式调用;
之前对ListView的BaseAdapter进行过封装,只需重写一个getView方法: 现在慢慢的RecyclerView成为主流,下面是RecyclerView.Adapter的封装: Base ...
随机推荐
- 分享一个随机更改 MAC地址 软件
有些软件 是根据 MAC地址 来判断 是不是 已经 安装过 这个 软件 (针对 有些软件是 可以 免费 使用的 ) 要想 一直 使用 的话 只需要 修改一下 mac地址 就可以 继续 使用! 在百度中 ...
- 手机应用PC端演示工具介绍
写给公司内部用的,所以没什么含量,请免炮轰. 为什么需要在PC端演示? 在Android及IOS系统上开发的手机应用,往往由于设备的限制,无法在演示汇报的场合向在场的众人展示界面,如果有工具可以将手机 ...
- Eclipse导入Android签名
本篇主要参照http://blog.csdn.net/wuxy_shenzhen/article/details/20946839 在安装安卓apk时经常会出现类似INSTALL_FAILED_SHA ...
- Python:认识变量和字符串
几个月前,我开始学习个人形象管理,从发型.妆容.服饰到仪表仪态,都开始做全新改造,在塑造个人风格时,最基础的是先了解自己属于哪种风格,然后找到参考对象去模仿,可以是自己欣赏的人.明星或模特等,直至最后 ...
- Swing学习篇 API之JButton组件
按钮(Jbutton) Swing中的按钮是Jbutton,它是javax.swing.AbstracButton类的子类,swing中的按钮可以显示图像,并且可以将按钮设置为窗口的默认图标,而且还可 ...
- highcharts的多级下钻以及图形形态转换
<script src="https://img.hcharts.cn/jquery/jquery-1.8.3.min.js"></script> < ...
- poj3320尺取法
Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...
- hdu4027线段树
https://vjudge.net/contest/66989#problem/H 此题真是坑到爆!!说好的四舍五入害我改了一个多小时,不用四舍五入!!有好几个坑点,注意要交换l,r的位置,还有输出 ...
- 配置web.xml和glassfish容器实现javaEE表单验证
web.xml配置: <!-- 声明用于安全约束的角色 --> <security-role> <role-name>ReimUser</role-name& ...
- MySQL 完整和增量备份与恢复
MySQL 完全备份与恢复 1.数据备份的重要性 在企业中数据的价值至关重要,数据保障了企业的业务的运行,因此数据的安全性及可靠性是运维的重中之重,任何数据的丢失都有可能会对企业产生严重的后果.造成数 ...