Cell定制和Frame模型引入
2017-05-10 本文已影响5人
一蓑丨烟雨
一、xib定制cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
MJStatusCell *cell = [MJStatusCell cellWithTableView:tableView];
// 2.在这个方法算好了cell的高度
cell.statusFrame = self.statusFrames[indexPath.row];
// 3.返回cell
return cell;
}
#import "MJTgCell.h"
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"tg";
MJTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
// 从xib中加载cell
cell = [[[NSBundle mainBundle] loadNibNamed:@"MJTgCell" owner:nil options:nil] lastObject];
}
return cell;
}
二、代码定制cell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"status";
MJStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[MJStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
return cell;
}
#import "MJStatusCell.h"
/**
* 构造方法(在初始化对象的时候会调用)
* 一般在这个方法中添加需要显示的子控件
*/
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// 1.头像
UIImageView *iconView = [[UIImageView alloc] init];
[self.contentView addSubview:iconView];
self.iconView = iconView;
// 2.昵称
UILabel *nameView = [[UILabel alloc] init];
nameView.font = MJNameFont;
[self.contentView addSubview:nameView];
self.nameView = nameView;
、、、
}
return self;
}
三、frame模型引入(自定义高度)
@interface MJStatus : NSObject
@property (nonatomic, copy) NSString *text; // 内容
@property (nonatomic, copy) NSString *icon; // 头像
@property (nonatomic, copy) NSString *name; // 昵称
@property (nonatomic, copy) NSString *picture; // 配图
@property (nonatomic, assign) BOOL vip;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)statusWithDict:(NSDictionary *)dict;
@implementation MJStatus
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)statusWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@class MJStatus;
@interface MJStatusFrame : NSObject
// 头像的frame
@property (nonatomic, assign, readonly) CGRect iconF;
// 昵称的frame
@property (nonatomic, assign, readonly) CGRect nameF;
// 会员图标的frame
@property (nonatomic, assign, readonly) CGRect vipF;
// 正文的frame
@property (nonatomic, assign, readonly) CGRect textF;
// 配图的frame
@property (nonatomic, assign, readonly) CGRect pictureF;
// cell的高度
@property (nonatomic, assign, readonly) CGFloat cellHeight;
@property (nonatomic, strong) MJStatus *status;
@end
@implementation MJStatusFrame
/**
* 计算文字尺寸
*
* @param text 需要计算尺寸的文字
* @param font 文字的字体
* @param maxSize 文字的最大尺寸
*/
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
NSDictionary *attrs = @{NSFontAttributeName : font};
return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
- (void)setStatus:(MJStatus *)status
{
_status = status;
// 子控件之间的间距
CGFloat padding = 10;
// 1.头像
CGFloat iconX = padding;
CGFloat iconY = padding;
CGFloat iconW = 30;
CGFloat iconH = 30;
_iconF = CGRectMake(iconX, iconY, iconW, iconH);
// 2.昵称
// 文字的字体
CGSize nameSize = [self sizeWithText:self.status.name font:MJNameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
CGFloat nameX = CGRectGetMaxX(_iconF) + padding;
CGFloat nameY = iconY + (iconH - nameSize.height) * 0.5;
_nameF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
// 3.会员图标
CGFloat vipX = CGRectGetMaxX(_nameF) + padding;
CGFloat vipY = nameY;
CGFloat vipW = 14;
CGFloat vipH = 14;
_vipF = CGRectMake(vipX, vipY, vipW, vipH);
// 4.正文
CGFloat textX = iconX;
CGFloat textY = CGRectGetMaxY(_iconF) + padding;
CGSize textSize = [self sizeWithText:self.status.text font:MJTextFont maxSize:CGSizeMake(300, MAXFLOAT)];
_textF = CGRectMake(textX, textY, textSize.width, textSize.height);
// 5.配图
if (self.status.picture) {// 有配图
CGFloat pictureX = textX;
CGFloat pictureY = CGRectGetMaxY(_textF) + padding;
CGFloat pictureW = 100;
CGFloat pictureH = 100;
_pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH);
_cellHeight = CGRectGetMaxY(_pictureF) + padding;
} else {
_cellHeight = CGRectGetMaxY(_textF) + padding;
}
}
#import "MJViewController.h"
for (NSDictionary *dict in dictArray) {
// 3.1.创建MJStatus模型对象
MJStatus *status = [MJStatus statusWithDict:dict];
// 3.2.创建MJStatusFrame模型对象
MJStatusFrame *statusFrame = [[MJStatusFrame alloc] init];
statusFrame.status = status;
}
#pragma mark - 实现数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.statusFrames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
MJStatusCell *cell = [MJStatusCell cellWithTableView:tableView];
// 2.在这个方法算好了cell的高度
cell.statusFrame = self.statusFrames[indexPath.row];
// 3.返回cell
return cell;
}
#pragma mark - 实现代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 取出这行对应的frame模型
MJStatusFrame *statusFrame = self.statusFrames[indexPath.row];
return statusFrame.cellHeight;
}