自定义UITableViewCell(一)

使用自定cell可以更加灵活的控制table中每个cell的布局显示等,因为table中的每个cell也是一个视图,可以自己控制往里面放什么样的视图。

使用xib和代码创建都大同小异。

  1. 首先新建一个viewController继承自UIViewController,并实现<UITableViewDataSource,UITableViewDelegate>这两个接口(代理和数据源)。
  2. 然后在xib文件中将一个table View视图拖入View中,并在链接检查器中将其数据源和代理连接至File's Owner指定代理和数据源位自身所对应的控制器。
  3. 新建一个cell继承自UITableViewCell,并新建一个Empty的User Interface。在该xib中只有File' owner和First Responder。从库中拖一个Table View Cell到GUI布局区中。然后选中该cell并选择属性检查器,填写Identifier值。该标示符用于在获取重用单元格时,确保得到正确的类型。
  4. 然后可以在该cell中自定义各种视图。并管理输出口到.h或.m文件中。并设置对应的属性。
  5. 重写属性的set方法。
  6. 实现viewController中的代理方法。

下面列出代码:

1 #import <UIKit/UIKit.h>
2 
3 @interface BIDViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
4 
5 @property (strong, nonatomic) NSArray *computers;
6 
7 @end
 1 #import "BIDViewController.h"
 2 #import "BIDNameAndColorCell.h"
 3 
 4 @interface BIDViewController ()
 5 
 6 @end
 7 
 8 @implementation BIDViewController
 9 
10 - (void)viewDidLoad
11 {
12     [super viewDidLoad];
13     // Do any additional setup after loading the view, typically from a nib.
14     
15     NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook",@"Name",@"White",@"Color", nil];
16     NSDictionary *row2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook Pro",@"Name",@"Siliver",@"Color", nil];
17     NSDictionary *row3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"iMac",@"Name",@"Siliver",@"Color", nil];
18     NSDictionary *row4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mac Mini",@"Name",@"Siliver11111111111111111111111111111111111111111111111111111111111Siliver11111111111111111111111111111111111111111111111111111111111Siliver11111111111111111111111111111111111111111111111111111111111Siliver11111111111111111111111111111111111111111111111111111111111Siliver11111111111111111111111111111111111111111111111111111111111Siliver11111111111111111111111111111111111111111111111111111111111Siliver11111111111111111111111111111111111111111111111111111111111",@"Color", nil];
19     NSDictionary *row5 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mac Pro",@"Name",@"Siliver",@"Color", nil];
20     
21     self.computers = [[NSArray alloc] initWithObjects:row1,row2,row3,row4,row5, nil];
22    
23 }
24 
25 -(void)viewDidUnload{
26     [super viewDidUnload];
27     self.computers = nil;
28 }
29 
30 - (void)didReceiveMemoryWarning
31 {
32     [super didReceiveMemoryWarning];
33     // Dispose of any resources that can be recreated.
34 }
35 
36 #pragma mark -
37 #pragma mark Table Data Source Methods
38 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
39     return [self.computers count];
40 }
41 
42 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
43     //cell的标示符
44     static NSString *CellTableIdentifier = @"CellTableIdentifier";
45     //用于辨别是否对该table注册了cell
46     static BOOL nibsRegistered = NO;
47     if (!nibsRegistered) {
48         //没有注册,则对该tableView注册一个指定的cell,一般只有第一次的时候注册
49         UINib *nib = [UINib nibWithNibName:@"BIDNameAndColorCell" bundle:nil];
50         [tableView registerNib:nib forCellReuseIdentifier:CellTableIdentifier];
51         nibsRegistered = YES;
52     }
53     //根据cell标示符,列出一个可重用的cell
54     BIDNameAndColorCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
55     if (cell == nil) {
56         //若已经没有可重用的cell了,就再创建一个
57         cell = [[BIDNameAndColorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier];
58     }
59     
60     //根据行数对cell进行赋值
61     NSUInteger row = [indexPath row];
62     NSDictionary *rowData = [self.computers objectAtIndex:row];
63     
64     cell.name = [rowData objectForKey:@"Name"];
65     cell.color = [rowData objectForKey:@"Color"];
66     
67     return cell;
68 }
69 
70 
71 #pragma mark -
72 #pragma mark Table Delegate Methods
73 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
74     NSInteger row =  [indexPath row];
75 
76     
77     NSString *colorStr = [[self.computers objectAtIndex:row] objectForKey:@"Color"];
78     NSString *nameStr = [[self.computers objectAtIndex:row] objectForKey:@"Name"];
79     
80     UIFont *font = [UIFont systemFontOfSize:17.0f];
81     CGSize size = CGSizeMake(212,MAXFLOAT);
82     //根据字体大小、允许的高度和宽度,计算出size
83     CGSize colorLabelSize = [colorStr sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByTruncatingTail];
84     
85     CGSize nameLabelSize = [nameStr sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByTruncatingTail];
86 
87 
88     NSLog(@"name, %f",nameLabelSize.height);
89     NSLog(@"color, %f",colorLabelSize.height);
90     
91     //返回改行内容的总高度
92     return colorLabelSize.height + nameLabelSize.height + 20 ;
93     
94 }
95 
96 @end
 1 #import <UIKit/UIKit.h>
 2 
 3 @interface BIDNameAndColorCell : UITableViewCell
 4 
 5 @property (copy, nonatomic) NSString *name;
 6 @property (copy, nonatomic) NSString *color;
 7 
 8 @property (strong, nonatomic) IBOutlet UILabel *nameLabel;
 9 @property (strong, nonatomic) IBOutlet UILabel *colorLabel;
10 
11 @end
 1 #import "BIDNameAndColorCell.h"
 2 
 3 @implementation BIDNameAndColorCell
 4 
 5 @synthesize name;
 6 @synthesize color;
 7 @synthesize nameLabel;
 8 @synthesize colorLabel;
 9 
10 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
11 {
12     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
13     if (self) {
14         // Initialization code
15     }
16     return self;
17 }
18 
19 - (void)setSelected:(BOOL)selected animated:(BOOL)animated
20 {
21     [super setSelected:selected animated:animated];
22 
23     // Configure the view for the selected state
24 }
25 
26 //重写set方法,在设置属性的同时,设置label的值
27 -(void)setName:(NSString *)n{
28     if(![n isEqualToString:name]){
29         name = [n copy];
30         nameLabel.text = name;
31     }
32 }
33 
34 -(void)setColor:(NSString *)c{
35     if(![c isEqualToString:color]){
36         color = [c copy];
37         colorLabel.text = color;
38     }
39 }
40 
41 @end
原文地址:https://www.cnblogs.com/ubersexual/p/2987028.html