您的位置:首页 > 产品设计 > UI/UE

UITableViewCell

2015-09-13 17:51 369 查看
一.自定义cell
1.新建继承于UITableViewCell 的子类
2.重写 初始化方法: (.m 文件)

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)identifier{
[self = super initWithStyle:style reuseIdentifier];
if(self){
[self createChild];
}
}


3.根据每行显示的内容 自定义创建里面的控件 (例如:)
.h文件


@property (nonatomic , retain)UILabel *titleLabel;
@property (nonatomic , retain)UIImageView *newsImageView;
@property (nonatomic , retain)UILabel *summaryLabel;
@property (nonatomic , retain)UILabel *timeLabel;

#pragma mark 返回高度的方法
+ (CGFloat)height;

#pragma mark 给 cell 进行赋值
- (void)setCellDataWithModel:(Data *)model;

#pragma mark 创建cell
+ (instancetype )createCellWithTableView:(UITableView *)tableView withIdentifier:(NSString *)identifier;


.m 文件

#define kScreenWidth  [UIScreen mainScreen].bounds.size.width

- (void)createChild{

self.titleLabel  = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, kScreenWidth - 20 * 2, 30)];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
[self.contentView addSubview:self.titleLabel];
[self.titleLabel release];

self.newsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.titleLabel.frame), CGRectGetMaxY(self.titleLabel.frame) +5 , CGRectGetWidth(self.titleLabel.frame) / 3 , 40)];
[self.contentView addSubview:self.newsImageView];
[self.newsImageView release];

self.summaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.newsImageView.frame) + 5, CGRectGetMinY(self.newsImageView.frame), CGRectGetWidth(self.titleLabel.frame) *2 / 3, CGRectGetHeight(self.newsImageView.frame))];
self.summaryLabel.alpha = 0.7;
self.summaryLabel.font = [UIFont systemFontOfSize:14];
self.summaryLabel.numberOfLines = 0;
[self.contentView addSubview:self.summaryLabel];
[self.summaryLabel release];

self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.summaryLabel.frame)+5, CGRectGetWidth(self.titleLabel.frame), 20)];    self.timeLabel.alpha = 0.7;    self.timeLabel.textAlignment = 2;    self.timeLabel.font = [UIFont systemFontOfSize:12];    [self.contentView addSubview:self.timeLabel];
[self.timeLabel release];
}

#pragma mark 返回高度的方法
+ (CGFloat)height;

#pragma mark 给 cell 进行赋值
- (void)setCellDataWithModel:(Data *)model{
self.titleLabel.text = model.title;
self.newsImageView.image = [UIImage imageNamed:@“4.jpg”];
self.summaryLabel.text = model.summary;

NSTimeInterval time = [model.lastUpdateTime  doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@“yyyy年MM月dd日 HH:mm”];
NSString *finalTime = [formatter StringFromDate:date];

self.timeLabel.text = finalTime;
}

#pragma mark 创建cell
+ (instancetype )createCellWithTableView:(UITableView *)tableView withIdentifier:(NSString *)identifier{
NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil){
cell = [[NewsCell alloc]initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:identifier];
}
return cell;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: