---
title: "新风作浪博客学习(十二)代码实现UITableViewCell表视图单元定制 ."
author: "android_zhang"
date: "2013-06-07T08:49:55Z"
tags: ["iOS"]
categories: ["移动开发"]
slug: "uitableviewcell"
draft: false
lastmod: "2013-01-19T11:49:33Z"
id: 1772442
url: "https://android-zhang.iteye.com/blog/1772442"
---

通常情况下我们会希望单元格UITableViewCell显示自定义不同数据，一般有两种方法，一种是通过代码给UITableViewCell在添加子视图，另一个就是用nib文件加载子视图；




本文是在iPhone4与iPad开发基础教程上的一个实例,因为纯代码编写和书上Xcode版本过老的问题，代码和书上有些细微不同，笔者为什么还写出来呢，因为在写的时候种种细节都是影响视图不能正确显示的原因，虽然是看着书上代码敲得的，运行和没有一个错误警告但是就是不能正确显示出来，我想大部分初学者和我一样的感受，以此文告诫自己:做一个细心的人；




1.新建工程名为TableViewCell ， File->New->Project ->single View Application -> next

![](http://dl.iteye.com/upload/attachment/0079/5224/70b4f622-c993-3898-a31f-fd0e71254792.png)






2.添加协议

```java
#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.表视图的初始化 

```java
- (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.委托方法

```java
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_computers count];
}
```



```java
-(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，两个静态，两个动态；动态的用于显示储存在字典数组中的数据;


对这行代码;有些疑问，然后查了一下contentView的定义，在UITableViewCell中这样定义，


![](http://dl.iteye.com/upload/attachment/0079/5226/d7df062e-1355-3c70-a75f-f23e0e86df08.png)


书上这样解释：表视图单元已经有了一个名为contentView的UIView子视图，用于对他的所有子视图惊喜分组，，所以我们再添加标签的时候，不用把标签作为子视图直接添加到表视图单元中，而是添加到contentView上；

在.h文件中我们看见了两个宏定义，然后在这段的代码中将宏定义的值赋给了两个label的tag，label的tag属性相当于标记的符号




5.运行结果截图

![](http://dl.iteye.com/upload/attachment/0079/5228/445dfa64-3669-391f-808e-41af4ca273ed.png)




![](http://dl.iteye.com/upload/attachment/0079/5230/9a446363-4b02-3d00-8a56-095976307b90.png)


5.运行结果截一张图就够了，为什么我截图截了两张，此处我想说说一下，单元格的重用问题；

蓝色部分表示的是被我选中的部分，但是蓝色下面的的单元格不能被选中，为什么，因为它并没有被创建，意思就是它的上面并