新风作浪博客学习(十二)代码实现UITableViewCell表视图单元定制 .

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


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


1.新建工程名为TableViewCell , File->New->Project ->single View Application -> next
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5224/70b4f622-c993-3898-a31f-fd0e71254792.png[/img]
[/img]


2.添加协议
#import <UIKit/UIKit.h>
#define kNameValueTag 1
#define kColorValueTag 2

@interface TVCViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UITableViewCell *tableViewCell;
@property (nonatomic,strong) NSArray *computers;

@end

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


@synthesize tableView = _tableView;

@synthesize tableViewCell = _tableViewCell;

@synthesize computers = _computers;


3.表视图的初始化
- (void)viewDidLoad
{
[super viewDidLoad];

_tableView = [[UITableView alloc] initWithFrame:self.view.frame];

self.tableView.delegate=self;
self.tableView.dataSource=self;
[self.view addSubview:_tableView];

NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook",@"Name",@"White",@"Color", nil];
NSDictionary *row2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook Pro",@"Name",@"Sliver",@"Color",nil];
NSDictionary *row3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"iMac",@"Name",@"Sliver",@"Color", nil];
NSDictionary *row4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mac Mini",@"Name",@"Sliver",@"Color", nil];
NSDictionary *row5 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mac Pro",@"Name",@"Sliver",@"Color", nil];

NSArray *array = [[NSArray alloc]initWithObjects:row1,row2,row3,row4,row5, nil];
_computers=array;

}


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


4.委托方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_computers count];
}


-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellTableIdentifier = @"CellTabeIndentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier];

CGRect nameLabelRect = CGRectMake(0, 5, 70, 15);
UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
nameLabel.textAlignment = UITextAlignmentRight;
nameLabel.text = @"Name:";
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:nameLabel];

CGRect colorLabelRect = CGRectMake(0, 26, 70, 15);
UILabel *colorLabel = [[UILabel alloc] initWithFrame:colorLabelRect];
colorLabel.textAlignment = UITextAlignmentRight;
colorLabel.text = @"Color:";
colorLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:colorLabel];

CGRect nameValueRect = CGRectMake(80, 5, 200, 15);
UILabel *nameValue = [[UILabel alloc] initWithFrame:nameValueRect];
nameValue.tag = kNameValueTag;
[cell.contentView addSubview:nameValue];

CGRect colorValueRect = CGRectMake(80, 25, 200, 15);
UILabel *colorValue = [[UILabel alloc] initWithFrame:colorValueRect];
colorValue.tag = kColorValueTag;
[cell.contentView addSubview:colorValue];

}

NSUInteger row = [indexPath row];
NSDictionary *rowData = [_computers objectAtIndex:row];
UILabel *name =(UILabel *)[cell.contentView viewWithTag:kNameValueTag];
name.text = [rowData objectForKey:@"Name"];

UILabel *color = (UILabel *)[cell.contentView viewWithTag:kColorValueTag];
color.text = [rowData objectForKey:@"Color"];

return cell;
}



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


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

[img]
[img]http://dl.iteye.com/upload/attachment/0079/5226/d7df062e-1355-3c70-a75f-f23e0e86df08.png[/img]
[/img]

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

在.h文件中我们看见了两个宏定义,然后在这段的代码中将宏定义的值赋给了两个label的tag,label的tag属性相当于标记的符号


5.运行结果截图
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5228/445dfa64-3669-391f-808e-41af4ca273ed.png[/img]
[/img]


[img]
[img]http://dl.iteye.com/upload/attachment/0079/5230/9a446363-4b02-3d00-8a56-095976307b90.png[/img]
[/img]

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

蓝色部分表示的是被我选中的部分,但是蓝色下面的的单元格不能被选中,为什么,因为它并没有被创建,意思就是它的上面并
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值