定制单元格

定制单元格




定制单元格的几种方式


通过UITableViewCell固定格式设置,其属性是imageView,textLabel、detailLabel,但他们的样式固定,且通常来说不易改变它们的位置,不够灵活


通过UITableViewCell的contentView属性添加子视图
使用xib自定义子视图,布局十分方便,开发较为迅速
子类化UITableViewCell,更加面向对象




固有样式位置  改变系统样式位置


- (void)layoutSubviews {


[super layoutSubviews];


self.textLabel.frame = CGRectMake(10, 5, 200, 20);


self.detailLabel.frame = CGRectMake(10, 30, 100, 10);


self.imageView.frame = CGRectMake(260, 30, 50, 10);


}




定制单元格——第一种方式


向contentView添加子视图


if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier] autorelease];






UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 5,
200, 20)];
titleLab.tag = 100;
titleLab.font = [UIFont boldSystemFontOfSize:14.0f];
titleLab.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:titleLab];
[titleLab release];
// 添加其他子视图.....
}
UILabel *titleLab = (UILabel *)[cell.contentView viewWithTag:100];
titleLab.text = @"label内容";




定义单元格——第二种方式


xib定义单元格


if (cell == nil) {


NSBundle *bundle = [NSBundle mainBundle];


// 加载xib


NSArray *array = [bundle loadNibNamed:@"newsCell" owner:self


options:nil];


cell = [array objectAtIndex:0];


}








UILabel *titleLab = (UILabel *)[cell.contentView viewWithTag:100];


titleLab.text = @"label内容";




定制单元格——第三种方式




子类化定制


- (void)_initViews {
_titleLab = [[UILabel alloc] initWithFrame:CGRectZero];
_titleLab.font = [UIFont boldSystemFontOfSize:14.0f];
_titleLab.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:_titleLab];
// .....初始化其他UI控件
}
- (void)setNews:(News *)news {
_titleLab.text = news.title;
_commentLab.text = [NSString stringWithFormat:@"%d条评论",
news.commentCount];
_timeLab.text = [NSString stringWithFormat:@"%d小时前",news.timeVal];
}
- (void)layoutSubviews {
[super layoutSubviews];
_titleLab.frame = CGRectMake(10, 5, 200, 20);
_commentLab.frame = CGRectMake(10, 30, 100, 10);
_timeLab.frame = CGRectMake(260, 30, 50, 10);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值