1. 每个cell的数据不是一样的。
2.所以每个cell的数据模型是不同的。
3.数据模型有的属性是相同的,所以可以将数据模型抽象出来,有一个父类,然后每个子类有特有的属性
4.cell样式是不同的,所以我们需要用nib文件来创建和封装cell
5.使用工厂模式,控制器分配任务定制不同样式的cell,还有定义不同的数据模型。
+ (id)newsWithDict:(NSDictionary *)dict {
YXNewsModel *newModel;
NSString *category = dict[@"category"];
if ([category isEqualToString:@"large"]) {
newModel = [YXLargeNewsModel newsModelWithDict:dict];
} else if ([category isEqualToString:@"list"]) {
newModel = [YXListNewsModel newsModelWithDict:dict];
} else if ([category isEqualToString:@"origin"]) {
newModel = [YXOriginNewsModel newsModelWithDict:dict];
} else if ([category isEqualToString:@"app"]) {
newModel = [YXAppNewsModel newsModelWithDict:dict];
}
return newModel;
}
+ (YXBaseCell *)newsCellWithTableView:(UITableView *)tableView newModel:(YXNewsModel *)newsModel {
YXBaseCell *baseCell;
NSString *category = [newsModel category];
if ([category isEqualToString:@"large"]) {
baseCell = [YXLargeCell cellFromNib:nil tableView:tableView];
} else if ([category isEqualToString:@"list"]) {
baseCell = [YXListCell cellFromNib:nil tableView:tableView];
} else if ([category isEqualToString:@"app"]) {
baseCell = [YXAppCell cellFromNib:nil tableView:tableView];
} else if ([category isEqualToString:@"origin"]) {
baseCell = [YXOriginCell cellFromNib:nil tableView:tableView];
}
baseCell.newsModel = newsModel;
return baseCell;
}
+ (id)cellFromNib:(NSString *)nibName tableView:(UITableView *)tableView {
NSString *className = NSStringFromClass([self class]);
// 三步运算符
nibName = !nibName ? className : nibName;
UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:nibName];
return [tableView dequeueReusableCellWithIdentifier:nibName];
}
#import "YXNewsModel.h"
@implementation YXNewsModel
+ (id)newsModelWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
- (id)initWithDict:(NSDictionary *)dict {
/**
如果应用KVC的方式进行属性赋值,一定要保证字典中有的key,在模型中必须对应属性,
如果模型中少了,就会直接报错
*/
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (id)newsModelWithDict:(NSDictionary *)dict {
YXLargeNewsModel *largeNewsModel = [super newsModelWithDict:dict];
// 1.计算标题frame值
CGFloat titleX = 8;
CGFloat titleY = 0;
CGFloat titleW = SCREEN_WIDTH-2*8;
// 动态计算高度的参考值
// 动态计算文字的高度,必须 与 nib 文件中显示的文字大小统一
CGSize size = CGSizeMake(titleW, CGFLOAT_MAX); // CGFLOAT_MAX 无限大
// 根据什么样的文字进行计算 attributes
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
size = [largeNewsModel.title boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
CGFloat titleH = size.height;
largeNewsModel.titleFrame = CGRectMake(titleX, titleY, titleW, titleH);
// 2.计算图片frame值
CGFloat picX = titleX;
// titleY + titleH
CGFloat picY = CGRectGetMaxY(largeNewsModel.titleFrame);
CGFloat picW = titleW;
CGFloat picH = 104;
largeNewsModel.pictureFrame = CGRectMake(picX, picY, picW, picH);
// 3.计算来源frame值
CGFloat sourceX = picX;
CGFloat sourceY = CGRectGetMaxY(largeNewsModel.pictureFrame);
CGFloat sourceW = 100;
CGFloat sourceH = 20;
largeNewsModel.sourceFrame = CGRectMake(sourceX, sourceY, sourceW, sourceH);
// 4.计算时间frame值
CGFloat timeX = 200;
CGFloat timeY = CGRectGetMaxY(largeNewsModel.pictureFrame);
CGFloat timeW = 100;
CGFloat timeH = 20;
largeNewsModel.timeFrame = CGRectMake(timeX, timeY, timeW, timeH);
// 5. cell的高度
largeNewsModel.cellHeight = CGRectGetMaxY(largeNewsModel.sourceFrame);
return largeNewsModel;
}
UITableView不同样式cell的封装
最新推荐文章于 2019-07-11 17:46:00 发布