代码实现UITableViewCell表视图单元定制

本文介绍如何使用纯代码在UITableViewCell中自定义布局,并展示了一个具体的示例。通过此方法,可以在单元格中添加多个UILabel来显示不同的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   通常情况下我们会希望单元格UITableViewCell显示自定义不同数据,一般有两种方法,一种是通过代码给UITableViewCell在添加子视图,另一个就是用nib文件加载子视图;


本文是在iPhone4与iPad开发基础教程上的一个实例,因为纯代码编写和书上Xcode版本过老的问题,代码和书上有些细微不同,笔者为什么还写出来呢,因为在写的时候种种细节都是影响视图不能正确显示的原因,虽然是看着书上代码敲得的,运行和没有一个错误警告但是就是不能正确显示出来,我想大部分初学者和我一样的感受,以此文告诫自己:做一个细心的人;


1.新建工程名为TableViewCell , File->New->Project ->single View Application -> next


2.添加协议

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2. #define kNameValueTag 1  
  3. #define kColorValueTag 2  
  4.   
  5. @interface TVCViewController : UIViewController  
  6. <UITableViewDataSource,UITableViewDelegate>  
  7.   
  8. @property (nonatomic,strong) UITableView *tableView;  
  9. @property (nonatomic,strong) UITableViewCell *tableViewCell;  
  10. @property (nonatomic,strong) NSArray *computers;  
  11.   
  12. @end  


并在.m文件的@implementation TVCViewController后面添加

@synthesize tableView = _tableView;

@synthesize tableViewCell = _tableViewCell;

@synthesize computers = _computers;


3.表视图的初始化

[cpp]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     _tableView = [[UITableView alloc] initWithFrame:self.view.frame];  
  6.       
  7.     self.tableView.delegate=self;  
  8.     self.tableView.dataSource=self;  
  9.     [self.view addSubview:_tableView];  
  10.       
  11.     NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook",@"Name",@"White",@"Color", nil];  
  12.     NSDictionary *row2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook Pro",@"Name",@"Sliver",@"Color",nil];  
  13.     NSDictionary *row3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"iMac",@"Name",@"Sliver",@"Color", nil];  
  14.     NSDictionary *row4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mac Mini",@"Name",@"Sliver",@"Color", nil];  
  15.     NSDictionary *row5 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mac Pro",@"Name",@"Sliver",@"Color", nil];  
  16.       
  17.     NSArray *array = [[NSArray alloc]initWithObjects:row1,row2,row3,row4,row5, nil];  
  18.     _computers=array;  
  19.       
  20. }  


_computers=array等价于self.computers = array;


4.委托方法

[cpp]  view plain copy
  1. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  2. {  
  3.     return [_computers count];  
  4. }  
[cpp]  view plain copy
  1. -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString *CellTableIdentifier = @"CellTabeIndentifier";  
  4.       
  5.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];  
  6.     if (cell == nil) {  
  7.         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier];  
  8.           
  9.         CGRect nameLabelRect = CGRectMake(0, 5, 70, 15);  
  10.         UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];  
  11.         nameLabel.textAlignment = UITextAlignmentRight;  
  12.         nameLabel.text = @"Name:";  
  13.         nameLabel.font = [UIFont boldSystemFontOfSize:12];  
  14.         [cell.contentView addSubview:nameLabel];  
  15.           
  16.         CGRect colorLabelRect = CGRectMake(0, 26, 70, 15);  
  17.         UILabel *colorLabel = [[UILabel alloc] initWithFrame:colorLabelRect];  
  18.         colorLabel.textAlignment = UITextAlignmentRight;  
  19.         colorLabel.text = @"Color:";  
  20.         colorLabel.font = [UIFont boldSystemFontOfSize:12];  
  21.         [cell.contentView addSubview:colorLabel];  
  22.           
  23.         CGRect nameValueRect = CGRectMake(80, 5, 200, 15);  
  24.         UILabel *nameValue = [[UILabel alloc] initWithFrame:nameValueRect];  
  25.         nameValue.tag = kNameValueTag;  
  26.         [cell.contentView addSubview:nameValue];  
  27.           
  28.         CGRect colorValueRect = CGRectMake(80, 25, 200, 15);  
  29.         UILabel *colorValue = [[UILabel alloc] initWithFrame:colorValueRect];  
  30.         colorValue.tag = kColorValueTag;  
  31.         [cell.contentView addSubview:colorValue];  
  32.           
  33.     }  
  34.        
  35.     NSUInteger row = [indexPath row];  
  36.     NSDictionary *rowData = [_computers objectAtIndex:row];  
  37.     UILabel *name =(UILabel *)[cell.contentView viewWithTag:kNameValueTag];  
  38.     name.text = [rowData objectForKey:@"Name"];  
  39.       
  40.     UILabel *color = (UILabel *)[cell.contentView viewWithTag:kColorValueTag];  
  41.     color.text = [rowData objectForKey:@"Color"];  
  42.       
  43.     return cell;  
  44. }  

本段代码实现的是在表视图单元内添加了4个UILabel,两个静态,两个动态;动态的用于显示储存在字典数组中的数据;

对这行代码[cell.contentView addSubview:nameLabel];有些疑问,然后查了一下contentView的定义,在UITableViewCell中这样定义,


书上这样解释:表视图单元已经有了一个名为contentView的UIView子视图,用于对他的所有子视图惊喜分组,,所以我们再添加标签的时候,不用把标签作为子视图直接添加到表视图单元中,而是添加到contentView上;

在.h文件中我们看见了两个宏定义,然后在这段的代码中将宏定义的值赋给了两个label的tag,label的tag属性相当于标记的符号,设置其他的整形数值都可以,就相当于学生的学号作用,通过label的tag属性同样能对label进行相关属性修改操作;


5.运行结果截图



5.运行结果截一张图就够了,为什么我截图截了两张,此处我想说说一下,单元格的重用问题;

蓝色部分表示的是被我选中的部分,但是蓝色下面的的单元格不能被选中,为什么,因为它并没有被创建,意思就是它的上面并没有UITableViewCell视图;





附上源代码:http://download.youkuaiyun.com/detail/duxinfeng2010/4417569

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值