FirstApp,iphone开发学习总结8,自定义TablevViewCell
2012-05-07 16:53
441 查看
创建TableViewCell文件(继承于UITableViewCell),在.h文件中添加2个label和一个方法:
@interface TableViewCell : UITableViewCell{
UILabel *nameLbl;
UILabel *ageLbl;
}
- (void)setValue:(NSString *)name age:(NSString *)age; 创建子视图://增加了2个label
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
nameLbl = [[UILabel alloc] initWithFrame:CGRectZero];
[[self contentView] addSubview:nameLbl];
[nameLbl release];
ageLbl = [[UILabel alloc] initWithFrame:CGRectZero];
[[self contentView] addSubview:ageLbl];
[ageLbl release];
}
return self;
} 子视图布局://只做了简单的处理,设置位置、改变字体大小和字体颜色
//此处layoutSubviews方法是得到view的具体大小后,再实现子类布局,这里直接设置了。
- (void)layoutSubviews
{
[super layoutSubviews];
nameLbl.frame = CGRectMake(5.0, 5.0, 20.0, 20.0);
nameLbl.font = [UIFont fontWithName:@"" size:18];
nameLbl.textColor = [UIColor redColor];
ageLbl.frame = CGRectMake(50.0, 5.0, 20.0, 20.0);
ageLbl.font = [UIFont fontWithName:@"" size:12];
ageLbl.textColor = [UIColor blueColor];
} 设置方法://设置
- (void)setValue:(NSString *)name age:(NSString *)age
{
[nameLbl setText:name];
[ageLbl setText:age];
}修改TableViewController.m,添加引用:
#import "TableViewCell.h" 修改- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FirstAppTableViewCell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setValue:[data objectAtIndex:[indexPath row]] age:[data objectAtIndex:[indexPath row]]];//直接用数字了
return cell;
}
编译运行,已经显示出2个不同颜色的label。
求指点~
@interface TableViewCell : UITableViewCell{
UILabel *nameLbl;
UILabel *ageLbl;
}
- (void)setValue:(NSString *)name age:(NSString *)age; 创建子视图://增加了2个label
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
nameLbl = [[UILabel alloc] initWithFrame:CGRectZero];
[[self contentView] addSubview:nameLbl];
[nameLbl release];
ageLbl = [[UILabel alloc] initWithFrame:CGRectZero];
[[self contentView] addSubview:ageLbl];
[ageLbl release];
}
return self;
} 子视图布局://只做了简单的处理,设置位置、改变字体大小和字体颜色
//此处layoutSubviews方法是得到view的具体大小后,再实现子类布局,这里直接设置了。
- (void)layoutSubviews
{
[super layoutSubviews];
nameLbl.frame = CGRectMake(5.0, 5.0, 20.0, 20.0);
nameLbl.font = [UIFont fontWithName:@"" size:18];
nameLbl.textColor = [UIColor redColor];
ageLbl.frame = CGRectMake(50.0, 5.0, 20.0, 20.0);
ageLbl.font = [UIFont fontWithName:@"" size:12];
ageLbl.textColor = [UIColor blueColor];
} 设置方法://设置
- (void)setValue:(NSString *)name age:(NSString *)age
{
[nameLbl setText:name];
[ageLbl setText:age];
}修改TableViewController.m,添加引用:
#import "TableViewCell.h" 修改- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FirstAppTableViewCell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setValue:[data objectAtIndex:[indexPath row]] age:[data objectAtIndex:[indexPath row]]];//直接用数字了
return cell;
}
编译运行,已经显示出2个不同颜色的label。
求指点~
相关文章推荐
- FirstApp,iphone开发学习总结5,Slider改变Label的值
- FirstApp,iphone开发学习总结11,表操作(移动、删除)
- FirstApp,iphone开发学习总结
- FirstApp,iphone开发学习总结2,简单的表
- FirstApp,iphone开发学习总结6,Navigation的使用
- FirstApp,iphone开发学习总结9,旋转
- FirstApp,iphone开发学习总结10,归档(序列化、固化)
- FirstApp,iphone开发学习总结13,方向感应和通告
- FirstApp,iphone开发学习总结1,UIview添加UIimage
- FirstApp,iphone开发学习总结7,相机
- FirstApp,iphone开发学习总结12,播放音乐、视频
- FirstApp,iphone开发学习总结0,创建TabBar
- FirstApp,iphone开发学习总结3,UIButton简单的操作
- FirstApp,iphone开发学习总结4,UITextField的值To UILabel
- 给iPhone应用开发的有志者的100个免费的课程和学习指南(100 Free Courses & Tutorials for Aspiring iPhone App Developers)
- PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [5] 版本设计分析及数据表设计
- 【IOS 开发学习总结-OC-41】★★ios开发——代码控制 UI 界面与 UI 控件的自定义
- iPhone开发入门系列1(iOS8+Swift版)天天打靶APP学习10-11