OC开发_代码片段——使用Xib自定义tableViewCell
一、实现步骤
1、新建一个XIB文件:描述cell——tableCell.xib
2、新建UITableViewCell的子类,也就是cell文件:封装XIB内部的所有东西——TestCell.m \Testcell.h
2.1 在cell文件中拥有XIB中的所有子控件 (包括生命属性,进行连线)
2.2 给cell增加模型属性,即通过重写set方法,根据模型属性设置cell内部子控件的属性 :
(这一步是从控制器解放抽取出来放在cell中)
2.3 提供一个类方法testCell,使得返回从XIB创建好的从 cell对象(也就是mainBundle):
(这一步是从控制器解放抽取出来放在cell中)
2.4 设置一个重用标识,Identifier
(这一步是从控制器解放抽取出来放在cell中)
2.5 设置cell的高度
(这一步是从控制器解放抽取出来放在cell中)
3、修改XIB中cell的类名即Class:使得XIB和cell相关联 ——TestCell
4、新建一个模型,即数据模型,使得封装数据 ——TestModel.m\TestModel.h
5、控制器取数
5.0 设置每一个section 有多少行:numberOfRowsInsection
5.1 使用重用标示取缓存池取得cell
5.2 如果缓存池没有cell,则创建一个cell ,这个就对应上面得TestCell
5.3 传递模型给cell (这里就需要在 cell中 @class TestModel; ,也就是对应上面得2.2set方法)
二、注意点:
1、缓存为空的时候,通过nsBundle,mainBundle获得数组(两种方法)
//方法1
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"tableCell" owner:nil options:nil];
cell = objects[]; //或者:方法2
UINib *nib = [UINib nibWithNibName:@"tableCell" bundle:nil];//nil则默认为mainBundle
NSArray *array = [nib instantiateWithOwner:nil options:nil];
cell = array[];
2、cell的高度,
技巧1:代理方法
技巧2:在viewDidLoad中 self.tableView.rowheight=80(适用于每行cell的高度相同)
3、循环利用缓存中的cell,必须 在Xib的identifier中设置和代码中一样的标识(XIB适用)
4、构造模型 构造方法 :自定义构造方法:必须调用父类的构造方法
三、代码实现
TestCell.m:
#import "TestCell.h"
#import "TestModel.h"
@implementation TestCell +(id) testCell
{
return [[NSBundle mainBundle] loadNibNamed:@"tableCell" owner:nil options:nil][];
}
-(void) setModel:(TestModel *)model
{
_model = model; _descLabel.text = _model.desc;
_nameLabel.text = _model.name;
_priceLabel.text = [NSString stringWithFormat:@"%d $",_model.price];
_iconImage.image = [UIImage imageNamed:_model.icon]; } +(NSString *) getID
{
return @"cell";
} @end
TestModel.m:
#import "TestModel.h" @implementation TestModel //自定义构造方法,必须调用父类的构造方法
-(id) initWithDict:(NSDictionary *)dict
{
if(self = [super init])
{
self.desc = dict[@"desc"];
self.name = dict[@"name"];
self.icon = dict[@"icon"];
self.price = [dict[@"price"] intValue];
}
return self;
} +(id) newsWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} @end
TestTableViewController.m:
#import "TestTableViewController.h"
#import "TestCell.h"
#import "TestModel.h" @interface TestTableViewController ()
{
NSMutableArray *_data; }
@end @implementation TestTableViewController - (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = ; //加载plist文件数据数组
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data.plist" ofType:nil]];
_data = [NSMutableArray array];
for (NSDictionary *arr in array) {
[_data addObject:[TestModel newsWithDict:arr]];
} } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return _data.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //1、定义缓存标识
//static NSString *ID = @"cell"; //2、从缓存池取出数据
TestCell *cell = [tableView dequeueReusableCellWithIdentifier:[TestCell getID]]; //3、判断是否油缓存可以取
if (cell == nil) { cell = [TestCell testCell];
} //4、传递模型数据
cell.model = _data[indexPath.row]; return cell;
} @end
源码下载:http://pan.baidu.com/s/1kTFuHwV
OC开发_代码片段——使用Xib自定义tableViewCell的更多相关文章
- OC开发_代码片段——代码编写自定义的tableViewCell
一.介绍 之前已经实现过通过简单的XIB文件来自定义我们的tableViewCell,包括每一步的步骤和代码:http://www.cnblogs.com/daomul/p/4355999.html ...
- OC开发_代码片段——代码编写简单的tableViewCell
许久前写的简单的tableView例子,主要针对处理缓存.协议.数据源datasource.局部刷新等问题进行解析. 其实这是一篇不全面的记录,只是用来记录一些备忘的东西,更全面的是使用TablVie ...
- 36个Android开发常用代码片段
//36个Android开发常用代码片段 //拨打电话 public static void call(Context context, String phoneNumber) { context.s ...
- 安卓开发_浅谈ListView(自定义适配器)
ListView作为一个实际开发中使用率非常高的视图,一般的系统自带的适配器都无法满足开发中的需求,这时候就需要开发人员来自定义适配器使得ListView能够有一个不错的显示效果 有这样一个Demo ...
- iOS开发-代码片段(Code Snippets)提高开发效率
简介 在 XCode4 引入了一个新特性,那就是“代码片段(Code Snippets)”.对于一些经常用到的代码,抽象成模板放到 Code Snippets 中,使用的时候就只需要键入快捷键就可以了 ...
- swift开发常用代码片段
// 绑定事件 cell.privacySwitch.addTarget(self, action: #selector(RSMeSettingPrivacyViewController.switch ...
- Android开发常用代码片段
拨打电话 public static void call(Context context, String phoneNumber) { context.startActivity( new Inten ...
- OC开发_整理笔记——多线程之GCD
一.进程和线程 二.各种队列! 1.GCD:Grand Central Dispatch 2.串行队列(Serial) 你可以创建任意个数的串行队列,每个队列依次执行添加的任务,一个队列 ...
- OC开发_整理笔记——友盟分享(社交化组件)
0.友盟的地址 http://dev.umeng.com,进入友盟,在使用友盟之前我们需要注册账号获取key (1 进入我们的产品,添加新应用 (2 输入信息,然后就会获取到key 1.选择社会化分 ...
随机推荐
- php BC高精确度函数库
#两个高精度数比较 #int bccomp ( string $left_operand , string $right_operand [, int $scale ] ) #$left=$right ...
- 没有启动 ASP.NET State service错误的解决方法
具体错误如下: 异常详细信息: System.Web.HttpException: 无法向会话状态服务器发出会话状态请求.请确保已启动 ASP.NET State service,并且客户端和服务器端 ...
- 如果通过html 链接打开app
https://my.oschina.net/liucundong/blog/354029 <!doctype html> <html> <head> <me ...
- 用R作Polar图等
用R作如下的各国Gini系数的Polar barChart: 作上图的R代码为: library(ggplot2) GiniData<- read.csv('IncomeInequality.c ...
- C/C++预处理指令
预处理指令 Preprocessor Directives define undef ifdef ifndef if endif else and elif line error include 预定 ...
- Hbase导入MapReduce数据的时候提示Running Job XXXX后就一直卡着不动
代码确信无误之后,ant运行起来,发现一执行就卡在Running Job XXXX那里一直不动了. 试着把代码打包成jar扔到Linux执行也还是一样的效果.还是停在那里.然后就一顿瞎蒙.最后发现是H ...
- selenium测试(Java)--下拉框(二十一)
例子: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...
- Why provision Bare Metal
Here are a few use-cases for bare metal (physical server) provisioning in cloud; there are doubtless ...
- Windows下配置Apache+PHP跑Wordpress拾遗
首先,我很少这么做,因为一旦有跑WAMP的需求,我就直接下一个wamp的安装包就可以了,市面上数不胜数,我一直用的是EasyPHP,不是说它有多好,而是很多年前第一次用后没什么问题,就一直用下来了.这 ...
- MySQL Server 5.7.13
如何安装MySQL,MySQL两种安装方式_百度经验 http://jingyan.baidu.com/article/cd4c2979033a17756f6e6047.html "C:\P ...