一、实现步骤

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的更多相关文章

  1. OC开发_代码片段——代码编写自定义的tableViewCell

    一.介绍 之前已经实现过通过简单的XIB文件来自定义我们的tableViewCell,包括每一步的步骤和代码:http://www.cnblogs.com/daomul/p/4355999.html ...

  2. OC开发_代码片段——代码编写简单的tableViewCell

    许久前写的简单的tableView例子,主要针对处理缓存.协议.数据源datasource.局部刷新等问题进行解析. 其实这是一篇不全面的记录,只是用来记录一些备忘的东西,更全面的是使用TablVie ...

  3. 36个Android开发常用代码片段

    //36个Android开发常用代码片段 //拨打电话 public static void call(Context context, String phoneNumber) { context.s ...

  4. 安卓开发_浅谈ListView(自定义适配器)

    ListView作为一个实际开发中使用率非常高的视图,一般的系统自带的适配器都无法满足开发中的需求,这时候就需要开发人员来自定义适配器使得ListView能够有一个不错的显示效果 有这样一个Demo ...

  5. iOS开发-代码片段(Code Snippets)提高开发效率

    简介 在 XCode4 引入了一个新特性,那就是“代码片段(Code Snippets)”.对于一些经常用到的代码,抽象成模板放到 Code Snippets 中,使用的时候就只需要键入快捷键就可以了 ...

  6. swift开发常用代码片段

    // 绑定事件 cell.privacySwitch.addTarget(self, action: #selector(RSMeSettingPrivacyViewController.switch ...

  7. Android开发常用代码片段

    拨打电话 public static void call(Context context, String phoneNumber) { context.startActivity( new Inten ...

  8. OC开发_整理笔记——多线程之GCD

    一.进程和线程   二.各种队列! 1.GCD:Grand Central Dispatch 2.串行队列(Serial)      你可以创建任意个数的串行队列,每个队列依次执行添加的任务,一个队列 ...

  9. OC开发_整理笔记——友盟分享(社交化组件)

    0.友盟的地址 http://dev.umeng.com,进入友盟,在使用友盟之前我们需要注册账号获取key (1 进入我们的产品,添加新应用 (2  输入信息,然后就会获取到key 1.选择社会化分 ...

随机推荐

  1. Storm ack和fail机制再论

    转自:http://www.cnblogs.com/fxjwind/p/3806404.html 之前对这个的理解有些问题,今天用到有仔细梳理了一遍,记录一下 首先开启storm tracker机制的 ...

  2. 创建以API为中心的Web应用(转)

    英文原文:Creating an API-Centric Web Application 引言 API——API是Application Programming Interface(应用编程接口)的简 ...

  3. 统计 fastq 文件 q20 , GC 含量的软件

    二代测序的分析过程中,经常需要统计原始下机数据的数据量,看数据量是否符合要求:另外还需要统计q20,q30,GC含量等反应测序质量的指标: 在kseq.h 的基础上稍加改造,就可以实现从fastq 文 ...

  4. 【Java面试题】36 List、Map、Set三个接口,存取元素时,各有什么特点?

    List与Set都是单列元素的集合,它们有一个功共同的父接口Collection. Set里面不允许有重复的元素, 存元素:add方法有一个boolean的返回值,当集合中没有某个元素,此时add方法 ...

  5. (转)mysql执行计划分析

    转自:https://www.cnblogs.com/liu-ke/p/4432774.html MySQL执行计划解读   Explain语法 EXPLAIN SELECT …… 变体: 1. EX ...

  6. SVN中Revert changes from this revision 跟Revert to this revision

    譬如有个文件,有十个版本,假定版本号是1,2,3,4,5,6,7,8,9,10. Revert to this revision: 如果是在版本6这里点击“Revert to this revisio ...

  7. JavaScript 取对象的值时用点和中括号的区别

    用点的时候 点后面是对象的真实属性名称, 用中括号的时候 中括号里面是变量或者字符串

  8. cesium图形上加载图片

    <!DOCTYPE html> <html> <head> <!-- Use correct character set. --> <meta c ...

  9. cocos2d-x 2.2 创建项目

    楼主用的是2.2版本号 曾经的版本号是要在vs中加入模版 建立项目 但新版本号更新后使用python建立项目 最好是python2.7以上 找到create_project.py文件所在路径  too ...

  10. swift--获取window

    有时候,我们需要频繁的调用界面,然后给当前页面加一个跟视图,这个时候就需要找windown, 代码如下: let rootVC = UIApplication.shared.delegate as! ...