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

来源:互联网 发布:网络赚钱qq群 编辑:程序博客网 时间:2024/05/15 04:37

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


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


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


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中这样定义,


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

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


5.运行结果截图



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

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





附上源代码:http://download.csdn.net/detail/duxinfeng2010/4417569