xib自定义非等高的cell

来源:互联网 发布:linux 局域网传文件 编辑:程序博客网 时间:2024/06/06 03:51
/* 1.在模型中增加一个 cellHeight属性,用来存放对于cell的高度 2.在cell的模型属性的set方法中调用[self layoutIfNeed]方法强制布局,然后计算出cellHeight的值 3.控制器中实现tableView:estimatedHeightForRowAtIndexPath方法,返回一个cell的估计高度,比如200 4.控制器中实现tableView:heightForRowAtIndexPath方法,返回cell的真实高度(模型中的cellHeight属性) */

////  WeiboCell.h#import <UIKit/UIKit.h>@class WeiboModel;@interface WeiboCell : UITableViewCell+ (instancetype)cellWithTableView:(UITableView *)tableView;/** *  模型属性 */@property(nonatomic,strong)WeiboModel *model;@end
////  WeiboCell.m#import "WeiboCell.h"#import "WeiboModel.h"@interface WeiboCell()@property (weak, nonatomic) IBOutlet UIImageView *iconView;@property (weak, nonatomic) IBOutlet UILabel *nameLabel;@property (weak, nonatomic) IBOutlet UIImageView *vipView;@property (weak, nonatomic) IBOutlet UILabel *contentLabel;@property (weak, nonatomic) IBOutlet UIImageView *picView;@end@implementation WeiboCell+ (instancetype)cellWithTableView:(UITableView *)tableView{    static NSString *ID = @"weibo";    WeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    if (cell == nil) {        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];    }        return cell;}- (void)awakeFromNib{    //设置label每一行文字的最大宽度    //让UILabel计算尺寸更加准确    self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;;}- (void)setModel:(WeiboModel *)model{    _model = model;        if (model.isVip) {        self.nameLabel.textColor = [UIColor orangeColor];        self.vipView.hidden = NO;    }else{        self.nameLabel.textColor = [UIColor blackColor];        self.vipView.hidden = YES;    }        self.iconView.image = [UIImage imageNamed:model.icon];    self.nameLabel.text = model.name;    self.contentLabel.text = model.text;    if (model.picture) {        self.picView.hidden = NO;        self.picView.image = [UIImage imageNamed:model.picture];    }else{        self.picView.hidden = YES;    }        //强制布局    [self layoutIfNeeded];        //计算cell的高度    if (self.picView.hidden) { //没有配图        model.cellHeight = CGRectGetMaxY(self.contentLabel.frame) + 10;    }else{ //有配图        model.cellHeight = CGRectGetMaxY(self.picView.frame) + 10;    }}@end
模型:

////  WeiboModel.h#import <UIKit/UIKit.h>@interface WeiboModel : NSObject@property (strong, nonatomic) NSString *name;@property (strong, nonatomic) NSString *text;@property (strong, nonatomic) NSString *icon;@property (strong, nonatomic) NSString *picture;@property (assign, nonatomic, getter=isVip) BOOL vip;+ (instancetype)weiboWithDict:(NSDictionary *)dict;/** *  cell的高度 */@property(nonatomic,assign)CGFloat cellHeight;@end
////  WeiboModel.m#import "WeiboModel.h"@implementation WeiboModel+ (instancetype)weiboWithDict:(NSDictionary *)dict{    WeiboModel *model = [[self alloc] init];    [model setValuesForKeysWithDictionary:dict];    return model;}@end

控制器里:
////  WeiboController.m#import "WeiboController.h"#import "WeiboModel.h"#import "WeiboCell.h"@interface WeiboController ()@property (strong, nonatomic) NSArray *weibos;@end@implementation WeiboController- (NSArray *)weibos{    if (_weibos == nil) {        // 加载plist中的字典数组        NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];                // 字典数组 -> 模型数组        NSMutableArray *ArrayM = [NSMutableArray array];        for (NSDictionary *dict in dictArray) {            WeiboModel *model = [WeiboModel weiboWithDict:dict];            [ArrayM addObject:model];        }                _weibos = ArrayM;    }    return _weibos;}- (void)viewDidLoad {    [super viewDidLoad];}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return self.weibos.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    WeiboCell *cell = [WeiboCell cellWithTableView:tableView];    cell.model = self.weibos[indexPath.row];        return cell;}#pragma mark - 代理方法/** *  返回每一行的高度 */- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    WeiboModel *model = self.weibos[indexPath.row];    return model.cellHeight;}/** *  估计cell的高度  *  只要返回了估计高度,那么就会先调用tableView:cellForRowAtIndexPath:创建cell, *  再调用tableView:heightForRowAtIndexPath:返回cell的真实高度 */- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 200;}@end
最后效果:






0 0
原创粉丝点击