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.mTestModel.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         //方法1
2         NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"tableCell" owner:nil options:nil];
3        cell = objects[0];
4         
5         //或者:方法2
6         UINib *nib =  [UINib nibWithNibName:@"tableCell" bundle:nil];//nil则默认为mainBundle
7        NSArray *array = [nib instantiateWithOwner:nil options:nil];
8        cell = array[0];

 2、cell的高度,

 技巧1:代理方法

 技巧2:在viewDidLoad中 self.tableView.rowheight=80(适用于每行cell的高度相同)

 3、循环利用缓存中的cell,必须 在Xib的identifier中设置和代码中一样的标识(XIB适用)

 4、构造模型 构造方法 :自定义构造方法:必须调用父类的构造方法

三、代码实现

TestCell.m:

 1 #import "TestCell.h"
 2 #import "TestModel.h"
 3 @implementation TestCell
 4 
 5 
 6 +(id) testCell
 7 {
 8     return [[NSBundle mainBundle] loadNibNamed:@"tableCell" owner:nil options:nil][0];
 9 }
10 -(void) setModel:(TestModel *)model
11 {
12     _model = model;
13     
14     _descLabel.text = _model.desc;
15     _nameLabel.text = _model.name;
16     _priceLabel.text = [NSString stringWithFormat:@"%d $",_model.price];
17     _iconImage.image = [UIImage imageNamed:_model.icon];
18     
19 }
20 
21 +(NSString *) getID
22 {
23     return @"cell";
24 }
25 
26 @end

TestModel.m:

 1 #import "TestModel.h"
 2 
 3 @implementation TestModel
 4 
 5 //自定义构造方法,必须调用父类的构造方法
 6 -(id) initWithDict:(NSDictionary *)dict
 7 {
 8     if(self = [super init])
 9     {
10         self.desc = dict[@"desc"];
11         self.name = dict[@"name"];
12         self.icon = dict[@"icon"];
13         self.price = [dict[@"price"] intValue];
14     }
15     return self;
16 }
17 
18 +(id) newsWithDict:(NSDictionary *)dict
19 {
20     return  [[self alloc] initWithDict:dict];
21 }
22 
23 @end

TestTableViewController.m:

 1 #import "TestTableViewController.h"
 2 #import "TestCell.h"
 3 #import "TestModel.h"
 4 
 5 @interface TestTableViewController ()
 6 {
 7     NSMutableArray *_data;
 8     
 9 }
10 @end
11 
12 @implementation TestTableViewController
13 
14 - (void)viewDidLoad {
15     [super viewDidLoad];
16     self.tableView.rowHeight = 80;
17     
18     //加载plist文件数据数组
19     NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data.plist" ofType:nil]];
20     _data = [NSMutableArray array];
21     for (NSDictionary *arr in array) {
22         [_data addObject:[TestModel newsWithDict:arr]];
23     }
24     
25 }
26 
27 - (void)didReceiveMemoryWarning {
28     [super didReceiveMemoryWarning];
29     // Dispose of any resources that can be recreated.
30 }
31 
32 #pragma mark - Table view data source
33 
34 
35 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
36 #warning Incomplete method implementation.
37     // Return the number of rows in the section.
38     return _data.count;
39 }
40 
41 
42 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
43     
44     //1、定义缓存标识
45     //static NSString *ID = @"cell";
46     
47     //2、从缓存池取出数据
48     TestCell *cell = [tableView dequeueReusableCellWithIdentifier:[TestCell getID]];
49     
50     //3、判断是否油缓存可以取
51     if (cell == nil) {
52 
53         cell = [TestCell testCell];
54     }
55     
56     //4、传递模型数据
57     cell.model = _data[indexPath.row];
58     
59     
60     return cell;
61 }
62 
63 @end

 源码下载:http://pan.baidu.com/s/1kTFuHwV

原文地址:https://www.cnblogs.com/daomul/p/4355999.html