代码实现UITableViewCell表视图单元定制
2014-04-23 17:17
357 查看
通常情况下我们会希望单元格UITableViewCell显示自定义不同数据,一般有两种方法,一种是通过代码给UITableViewCell在添加子视图,另一个就是用nib文件加载子视图;
本文是在iPhone4与iPad开发基础教程上的一个实例,因为纯代码编写和书上Xcode版本过老的问题,代码和书上有些细微不同,笔者为什么还写出来呢,因为在写的时候种种细节都是影响视图不能正确显示的原因,虽然是看着书上代码敲得的,运行和没有一个错误警告但是就是不能正确显示出来,我想大部分初学者和我一样的感受,以此文告诫自己:做一个细心的人;
1.新建工程名为TableViewCell , File->New->Project ->single View Application -> next
2.添加协议
[cpp] view
plaincopy
#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.表视图的初始化
[cpp] view
plaincopy
- (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.委托方法
[cpp] view
plaincopy
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_computers count];
}
[cpp] view
plaincopy
-(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
本文是在iPhone4与iPad开发基础教程上的一个实例,因为纯代码编写和书上Xcode版本过老的问题,代码和书上有些细微不同,笔者为什么还写出来呢,因为在写的时候种种细节都是影响视图不能正确显示的原因,虽然是看着书上代码敲得的,运行和没有一个错误警告但是就是不能正确显示出来,我想大部分初学者和我一样的感受,以此文告诫自己:做一个细心的人;
1.新建工程名为TableViewCell , File->New->Project ->single View Application -> next
2.添加协议
[cpp] view
plaincopy
#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.表视图的初始化
[cpp] view
plaincopy
- (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.委托方法
[cpp] view
plaincopy
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_computers count];
}
[cpp] view
plaincopy
-(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
相关文章推荐
- 代码实现UITableViewCell表视图单元定制
- 代码实现UITableViewCell表视图单元定制
- 代码实现Cell的自定制
- 利用包实现视图内容的不同展现(续new)——前台调用,给出代码部分
- iOS:删除storyBoard,纯代码实现UITabBarController的视图切换功能
- 创建表单-定制视图单元
- 用Kotlin实现Android定制视图(KAD 06)
- 实现前后视图的C#代码
- 图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
- 图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
- Qt 学习之路2(46)视图和委托---代码实现
- (iOS)iPhone手机程序源码:全代码实现选择视图界面NSArray
- Android常用三栏式滑动/滚动视图(View)的设计与实现代码
- 【ios学习记录】- 通过nib文件实现自定义表视图单元
- VIM语法高亮、VIM代码补全、VIM结构化视图功能的配置实现(1)
- Android视图SurfaceView的实现原理分析(示例,出错代码)
- 一句代码实现带有头视图的pageController效果
- 图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
- 代码 + storyBoard拖拽法,实现手动切换视图