UITableView不同样式cell的封装

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#import "LHDBaseTableViewCell.h" @interface LHDBaseTableView : UITableView <UITableViewDataSource,UITableViewDelegate> @property (nonatomic, assign) CGFloat cellHeight; @property (nonatomic, assign) BOOL fixed; //是否固定高度 @property (nonatomic, copy) NSInteger(^tableViewNumberOfRowInSection)(UITableView *,NSInteger); @property (nonatomic, copy) UITableViewCell *(^tableViewCellForRowAtIndexPath)(UITableView *, NSIndexPath *); @property (nonatomic, copy) NSInteger(^numberOfSectionInTabelView)(UITableView *); @property (nonatomic, copy) CGFloat(^tableViewHeightForRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) BOOL(^tableViewCanEditRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) void(^tableViewCommitEditingStyleforRowAtIndexPath)(UITableView *,UITableViewCellEditingStyle,NSIndexPath *); @property (nonatomic, copy) UITableViewCellEditingStyle (^tableViewEditingStyleForRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) void(^tableViewDidSelectRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) void(^tableViewDidDeselectRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) UIView *(^tableViewViewForHeaderInSection)(UITableView *,NSInteger); @property (nonatomic, copy) CGFloat(^tableViewHeightForHeaderInSection)(NSInteger); @property (nonatomic, strong) LHDBaseTableViewCell *myTableViewCell; @property (nonatomic, strong) NSArray *dataArray; - (void)resetDelegate;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值