zoukankan      html  css  js  c++  java
  • 通过xib自定义UITableViewCell

    通过xib自定义UITableViewCell

    一、新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCellDemo,则完成后自动生成代码视图如下图:

    二。新建一个UITableViewCell文件:

     三。Add---New Files----User Interface-----Empty XIB

           创建一个空的  MyTableViewCell.xib 文件,记住,XIB的名称一定要跟 签名的类的名称一致,也就是一模一样。

           一定要选 Empty XIB类型,如果不是选的这个,那么创建的XIB里面的已经存在的那个UIView将不能调整高度,它的高度固定死了。

     

          

    4.在xib中拖入一个Table View Cell 和一个label 一个imageView ,并于MyTableViewCell中连接如下图:

          五。这样,就可以往这个新添加的View里面添加我们自己的个性化控件了,这个View就是我们的Cell的模板了。这个过程跟普通的XIB一样,没有什么特别的。

         那么如何在代码中使用这个MyTableViewCell呢?

         代码如下:

    MyTableViewCell类:

    1. //  MyTableViewCell.h  
    2. //  UITableViewCellDemo  
    3. //  
    4. //  Created by WildCat on 13-8-6.  
    5. //  Copyright (c) 2013年 wildcat. All rights reserved.  
    6. //  
    7.   
    8. #import <UIKit/UIKit.h>  
    9.   
    10. @interface MyTableViewCell : UITableViewCell  
    11.   
    12. @property (weak, nonatomic) IBOutlet UIImageView *imageView;  
    13.   
    14. @property (weak, nonatomic) IBOutlet UILabel *titleLabel;  
    15. @property (copy,nonatomic) NSString *titleName;  
    16. @property (copy,nonatomic) NSString *image;  
    17. @end  
    1. //  
    2. //  MyTableViewCell.m  
    3. //  UITableViewCellDemo  
    4. //  
    5. //  Created by WildCat on 13-8-6.  
    6. //  Copyright (c) 2013年 wildcat. All rights reserved.  
    7. //  
    8.   
    9. #import "MyTableViewCell.h"  
    10.   
    11. @implementation MyTableViewCell  
    12. @synthesize imageView;  
    13. @synthesize titleLabel;  
    14. @synthesize titleName;  
    15. @synthesize image;  
    16.   
    17. -(void)setImage:(NSString *)image{  
    18.     self.imageView.image=[UIImage imageNamed:[image copy]];  
    19.   
    20. }  
    21. -(void)setTitleName:(NSString *)titleName{  
    22.     self.titleLabel.text=[titleName copy];  
    23.   
    24. }  
    25.   
    26.   
    27.   
    28. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
    29. {  
    30.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
    31.     if (self) {  
    32.         // Initialization code  
    33.     }  
    34.     return self;  
    35. }  
    36.   
    37. - (void)setSelected:(BOOL)selected animated:(BOOL)animated  
    38. {  
    39.     [super setSelected:selected animated:animated];  
    40.   
    41.     // Configure the view for the selected state  
    42. }  
    43.   
    44. @end  

    ViewController类文件:

    1. //  ViewController.h  
    2. //  UITableViewCellDemo  
    3. //  
    4. //  Created by WildCat on 13-8-6.  
    5. //  Copyright (c) 2013年 wildcat. All rights reserved.  
    6. //  
    7.   
    8. #import <UIKit/UIKit.h>  
    9.   
    10. @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>  
    11. @property (nonatomic,strong) UITableView *myTableView;  
    12.   
    13. @end  
    1. //  
    2. //  ViewController.m  
    3. //  UITableViewCellDemo  
    4. //  
    5. //  Created by WildCat on 13-8-6.  
    6. //  Copyright (c) 2013年 wildcat. All rights reserved.  
    7. //  
    8.   
    9. #import "ViewController.h"  
    10. #import "MyTableViewCell.h"  
    11. @interface ViewController ()  
    12.   
    13. @end  
    14.   
    15. @implementation ViewController  
    16. @synthesize myTableView=_myTableView;  
    17.   
    18. #pragma mark -实现协议方法  
    19. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{  
    20.     return 1;  
    21. }  
    22. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
    23.     return 5;  
    24.   
    25. }  
    26. - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
    27.     CGFloat result = 40.0f;  
    28.     if ([tableView isEqual:self.myTableView]){  
    29.         result = 80.0f;  
    30.     }  
    31.     return result;  
    32. }  
    33. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
    34.   
    35.      
    36.     MyTableViewCell *cell;  
    37.     //定义CustomCell的复用标识,这个就是刚才在CustomCell.xib中设置的那个Identifier,一定要相同,否则无法复用  
    38.     static NSString *identifier = @"MyTableViewCell";  
    39.     //根据复用标识查找TableView里是否有可复用的cell,有则返回给cell  
    40.     cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier];  
    41.     //判断是否获取到复用cell,没有则从xib中初始化一个cell  
    42.     if (!cell) {  
    43.         //将Custom.xib中的所有对象载入  
    44.         NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:nil options:nil];  
    45.         //第一个对象就是CustomCell了  
    46.         cell = [nib objectAtIndex:0];  
    47.     }  
    48.       
    49.     cell.image=@"1.jpeg";  
    50.     cell.titleName=@"wildcat的专栏 新浪微博:http://weibo.com/u/3202802157";  
    51.      return cell;  
    52. }  
    53.   
    54. #pragma mark - Controller方法  
    55. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
    56. {  
    57.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
    58.     if (self) {  
    59.         // Custom initialization  
    60.     }  
    61.     return self;  
    62. }  
    63.   
    64. - (void)viewDidLoad  
    65. {  
    66.     [super viewDidLoad];  
    67.     // Do any additional setup after loading the view.  
    68.     self.view.backgroundColor=[UIColor redColor];  
    69.       
    70.     self.myTableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];  
    71.     self.myTableView.dataSource=self;  
    72.     self.myTableView.delegate=self;  
    73.     self.myTableView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;  
    74.       
    75.     [self.view addSubview:self.myTableView];  
    76.       
    77. }  
    78.   
    79. - (void)viewDidUnload  
    80. {  
    81.     [super viewDidUnload];  
    82.     // Release any retained subviews of the main view.  
    83.     self.myTableView=nil;  
    84. }  
    85.   
    86. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
    87. {  
    88.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
    89. }  
    90.   
    91. @end  


    运行结果:

  • 相关阅读:
    洛谷$P4768 [NOI2018]$归程 $kruscal$重构树
    洛谷$P2469 [SDOI2010]$ 星际竞速 网络流
    洛谷$P2572 [SCOI2010]$ 序列操作 线段树/珂朵莉树
    $CF914D Bash and a Tough Math Puzzle$ 线段树
    洛谷$P2824 [HEOI2016/TJOI2016]$ 排序 线段树+二分
    洛谷$P$4137 $Rmq Problem / mex$ 主席树
    bat语法
    zabbix监控oracle
    sqlserver常用命令-4
    sqlserver库相关-表相关-3
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3287983.html
Copyright © 2011-2022 走看看