从nib文件加载自定义的UITableviewCell

[csharp]  view plain copy
  1. 也许我们很多人都清楚怎么定制属于我们自己的UItableviewcell, 习惯了代码的我,竟然今天才彻底的理解了从nib文件加载自己定制的cell。  
[javascript]  view plain copy
  1. registerNib:forCellReuseIdentifier:  
  2. Registers a nib object containing a cell with the table view under a specified identifier.  
  3.   
  4. - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier  
  5. Parameters  
  6. nib  
  7. A nib object that specifies the nib file to use to create the cell. This parameter cannot be nil.  
  8. identifier  
  9. The reuse identifier for the cell. This parameter must not be nil and must not be an empty string.  
  10. Discussion  
  11. Prior to dequeueing any cells, call this method or the registerClass:forCellReuseIdentifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.  
  12.   
  13. If you previously registered a class or nib file with the same reuse identifier, the nib you specify in the nib parameter replaces the old entry. You may specify nil for nib if you want to unregister the nib from the specified reuse identifier.  
  14.   
  15. Availability  
  16. Available in iOS 5.0 and later.  
  17. See Also  
  18. tableView:cellForRowAtIndexPath: (UITableViewDataSource)  
  19. Declared In  
  20. UITableView.h  

从官网的文档可以看出,通过 registerNib:forCellReuseIdentifier 这个方法可以实现。在使用复用机制之前要先确保 registernib这个方法,这个方法就是告诉了table是通过nib文件来创建cell的,这点很重要。

下面贴上主要的代码,这个是.h文件

[csharp]  view plain copy
  1. //  
  2. //  ViewController.h  
  3. //  TestCell  
  4. //  
  5. //  Created by Juncy_Fan on 13-3-11.  
  6. //  Copyright (c) 2013年 Juncy_Fan. All rights reserved.  
  7. //  
  8.  
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{  
  12.     UITableView *table;  
  13.     UINib *nib;  
  14. }  
  15.   
  16. @end  

下面的是.m文件


[csharp]  view plain copy
  1. //  
  2. //  ViewController.m  
  3. //  TestCell  
  4. //  
  5. //  Created by Juncy_Fan on 13-3-11.  
  6. //  Copyright (c) 2013年 Juncy_Fan. All rights reserved.  
  7. //  
  8.  
  9. #import "ViewController.h"  
  10. #import "MyCell.h"  
  11.   
  12. @interface ViewController ()  
  13.   
  14. @end  
  15.   
  16. @implementation ViewController  
  17. static BOOL nibsRegistered;  
  18. - (void)viewDidLoad  
  19. {  
  20.     [super viewDidLoad];  
  21.     nibsRegistered = NO;  
  22.     NSLog(@"初始化:%d",nibsRegistered);  
  23.     table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];  
  24.     [self.view addSubview:table];  
  25.     table.delegate = self;  
  26.     table.dataSource = self;  
  27.     [table release];  
  28. }  
  29.   
  30. - (void)didReceiveMemoryWarning  
  31. {  
  32.     [super didReceiveMemoryWarning];  
  33.     // Dispose of any resources that can be recreated.  
  34. }  
  35.   
  36. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  37.     return 300;  
  38. }  
  39.   
  40. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  41.     static NSString *identy = @"CustomCell";  
  42.     if (!nib) {  
  43.         nib = [UINib nibWithNibName:@"MyCell" bundle:nil];  
  44.         [tableView registerNib:nib forCellReuseIdentifier:identy];  
  45.         NSLog(@"我是从nib过来的,%d",indexPath.row);  
  46.     }  
  47.     MyCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];  
  48.     NSUInteger row = [indexPath row];  
  49.     if (row%2 == 0) {  
  50.         cell.myTitle.text = @"我是偶数行的";  
  51.         cell.mySubTitle.text = @"我是子标题";  
  52.         cell.imageicon.image = [UIImage imageNamed:@"201202.png"];  
  53.     }else{  
  54.         cell.myTitle.text = @"我是奇数行的";  
  55.         cell.mySubTitle.text = @"我是奇数行的子标题";  
  56.         cell.imageicon.image = [UIImage imageNamed:@"201203.png"];  
  57.     }  
  58.     return cell;  
  59. }  
  60. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  61.     return 100;  
  62. }  
  63.   
  64. @end  

ok啦,定制cell的时候,nib文件里面identifer一定要和代码里面的保持一致哦。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值