zoukankan      html  css  js  c++  java
  • ios之UITabelViewCell的自定义(xib实现)

    通过继承UITableViewCell来自定义cell

     

    1、创建一个空的项目、命名:


     

    2、创建一个UITableViewController 并且同时创建xib:


     

    3、设置AppDelegate.m中window的根控制器为刚刚创建的TableViewController:

     

    1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    2. {  
    3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
    4.     TableViewController *tableViewController = [[[TableViewController alloc] init] autorelease]; //自动释放  
    5.     //设置根控制器  
    6.     self.window.rootViewController = tableViewController;  
    7.     [self.window makeKeyAndVisible];  
    8.     return YES;  
    9. }  


    4、创建自定义的UITableViewCell:

     


     

    5、创建自定义cell的xib 拖放需要的控件

    选择User Interface。创建空的xib。拖入Cell控件。

    完成自定义的cell控件。

     

    设置cell控件的Identfier。绑定Cell类并且将控件的输出口关联到TableViewCell.h文件中。

     

    6、对TableViewController类编码,在委托方法中设置自定义的Cell:

     

    1. #import "TableViewController.h"  
    2. #import "TableViewCell.h"  
    3.   
    4. @interface TableViewController (){  
    5.     NSMutableArray *tableData;  //表格数据  
    6. }  
    7.   
    8. @end  
    9.   
    10. @implementation TableViewController  
    11.   
    12. - (id)initWithStyle:(UITableViewStyle)style  
    13. {  
    14.     self = [super initWithStyle:style];  
    15.     if (self) {  
    16.         // Custom initialization  
    17.     }  
    18.     return self;  
    19. }  
    20.   
    21. - (void)viewDidLoad  
    22. {  
    23.     [super viewDidLoad];  
    24.     //初始化表格数据  
    25.     tableData = [[NSMutableArray alloc] init];  
    26.     for (int i = 0; i< 10; i++) {  
    27.         [tableData addObject:[NSString stringWithFormat:@"MyCellDemon%i",i]];  
    28.     }  
    29.   
    30.     //设置row的高度为自定义cell的高度  
    31.     self.tableView.rowHeight = 90;  
    32.    
    33. }  
    34.   
    35. - (void)didReceiveMemoryWarning  
    36. {  
    37.     [super didReceiveMemoryWarning];  
    38.  }  
    39.   
    40. #pragma mark - Table view data source  
    41.   
    42. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
    43. {  
    44. #warning Potentially incomplete method implementation.  
    45.      return 1;  
    46. }  
    47.   
    48. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
    49. {  
    50. #warning Incomplete method implementation.  
    51.      return [tableData count];  
    52. }  
    53.   
    54. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
    55. {  
    56.     //指定cellIdentifier为自定义的cell  
    57.     static NSString *CellIdentifier = @"TableViewCell";  
    58.     //自定义cell类  
    59.     TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    60.     if (cell == nil) {  
    61.         //通过xib的名称加载自定义的cell  
    62.         cell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil] lastObject];  
    63.     }  
    64.       
    65.     //添加测试数据  
    66.     cell.titleLabel.text = [tableData objectAtIndex:indexPath.row];  
    67.     cell.content.text = @"这是一些测试数据";  
    68.     //测试图片  
    69.     cell.iamge.image = [UIImage imageNamed:@"testImage.jpg"];  
    70.      return cell;  
    71. }  
    72.   
    73. #pragma mark - Table view delegate  
    74.   
    75. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
    76. {  
    77.   
    78. }  
    79.   
    80. @end  

    最终效果:

  • 相关阅读:
    Windows 7目录
    用wubi安装的Ubuntu在重装Windows 7系统后,如何恢复(转)
    用java查询HBase中某表的一批数据
    hbase数据模型以及编码压缩(转)
    应用Flume+HBase采集和存储日志数据
    HBase性能调优(转)
    【剑指Offer学习】【面试题50:树中两个结点的最低公共祖先】
    [Phonegap+Sencha Touch] 移动开发24 打包wp8.1的App,执行时输入框聚焦弹出软键盘之后,界面上移而不恢复原位的解决的方法
    写在课程设计之后
    String内存溢出异常(错误)可能的原因及解决方式
  • 原文地址:https://www.cnblogs.com/yulang314/p/3550487.html
Copyright © 2011-2022 走看看