定制单元格
定制单元格的几种方式
通过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);
}
定制单元格的几种方式
通过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);
}