一、實現步驟
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 //方法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