frame方式
- mode部分代码
@interface XMGStatus : NSObject
/** 文字/图片数据***/
/** 图像 */
@property (nonatomic, copy) NSString *icon;
/** 昵称 */
@property (nonatomic, copy) NSString *name;
/** 正文(内容) */
@property (nonatomic, copy) NSString *text;
/** VIP */
@property (nonatomic, assign, getter=isVip) BOOL vip;
/** 配图 */
@property (nonatomic, copy) NSString *picture;
/** frame数据***/
/** 图像的frame */
@property (nonatomic, assign) CGRect iconFrame;
/** 昵称的frame */
@property (nonatomic, assign) CGRect nameFrame;
/** vip的frame */
@property (nonatomic, assign) CGRect vipFrame;
/** 正文frame */
@property (nonatomic, assign) CGRect textFrame;
/** 配图的frame */
@property (nonatomic, assign) CGRect pictureFrame;
/** cell的高度 */
@property (nonatomic, assign) CGFloat cellHeight;
@end
- cellHeight获取方方法
/** 图像 */
CGFloat iconX = space;
CGFloat iconY = space;
CGFloat iconWH = 30;
CGFloat space = 10;
self.iconFrame = CGRectMake(iconX, iconY, iconWH, iconWH);
/** 昵称 */
CGFloat nameX = CGRectGetMaxX(self.iconFrame) + space;
CGFloat nameY = iconY;
NSDictionary *nameAtt = @{NSFontAttributeName : [UIFont systemFontOfSize:17]};
// 计算昵称文字的尺寸
CGSize nameSize = [self.name sizeWithAttributes:nameAtt];
CGFloat nameW = nameSize.width;
CGFloat nameH = nameSize.height;
self.nameFrame = CGRectMake(nameX, nameY, nameW, nameH);
/** vip */
if (self.isVip) {
CGFloat vipX = CGRectGetMaxX(self.nameFrame) + space;
CGFloat vipW = 14;
CGFloat vipH = nameH;
CGFloat vipY = nameY;
self.vipFrame = CGRectMake(vipX, vipY, vipW, vipH);
}
/** 正文 */
CGFloat textX = iconX;
CGFloat textY = CGRectGetMaxY(self.iconFrame) + space;
CGFloat textW = [UIScreen mainScreen].bounds.size.width - 2 * space;
NSDictionary *textAtt = @{NSFontAttributeName : [UIFont systemFontOfSize:14]};
// 最大宽度是textW,高度不限制
CGSize textSize = CGSizeMake(textW, MAXFLOAT);
CGFloat textH = [self.text boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:textAtt context:nil].size.height;
self.textFrame = CGRectMake(textX, textY, textW, textH);
/** 配图 */
if (self.picture) { // 有配图
CGFloat pictureWH = 100;
CGFloat pictureX = iconX;
CGFloat pictureY = CGRectGetMaxY(self.textFrame) + space;
self.pictureFrame = CGRectMake(pictureX, pictureY, pictureWH, pictureWH);
_cellHeight = CGRectGetMaxY(self.pictureFrame) + space;
} else {
_cellHeight = CGRectGetMaxY(self.textFrame) + space;
}
}
return _cellHeight;
}
- cell获取高度的方式
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"heightForRowAtIndexPath--%zd",indexPath.row);
XMGStatus *status = self.statuses[indexPath.row];
return status.cellHeight;
}
- cell 里面操作
self.pictureImageView.hidden = NO;
self.pictureImageView.image = [UIImage imageNamed:status.picture];
self.pictureImageView.hidden = YES;
self.iconImageView.frame = self.status.iconFrame;
self.nameLabel.frame = self.status.nameFrame;
self.vipImageView.frame = self.status.vipFrame;
self.text_Label.frame = self.status.textFrame;
self.pictureImageView.frame = self.status.pictureFrame;
storyboard 方式
-
IOS8 之后只适合简单的cell
// self-sizing(iOS8 以后)
// 告诉tableView所有cell的真实高度是自动计算的(根据设置的约束)
self.tableView.rowHeight = UITableViewAutomaticDimension;
// 设置估算高度
self.tableView.estimatedRowHeight = 44;
- cell 里面操作
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pitureHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureBottom;
if (status.picture) { // 有配图
self.pictureImageView.hidden = NO;
self.pictureImageView.image = [UIImage imageNamed:status.picture];
self.pitureHeight.constant = 100;
self.pictureBottom.constant = 10;
} else { // 无配图
self.pictureImageView.hidden = YES;
self.pitureHeight.constant = 0;
self.pictureBottom.constant = 0;
}
-
IOS8 之前的操作方式
// 仍然需要设置估算高度 (减少tableView:heightForRowAtIndexPath:的调用次数)
self.tableView.estimatedRowHeight = 200;
计算cell的任务交给cell自己
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 创建一个临时的cell(目的:传递indexPath对应这行的模型,布局内部所有的子控件,得到cell的高度)
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:ID];
}
// 传递模型数据
cell.status = self.statuses[indexPath.row];
return cell.cellHeight;
}
(CGFloat)cellHeight
{
// 强制刷新(label根据约束自动计算它的宽度和高度)
[self layoutIfNeeded];
CGFloat cellHeight = 0;
if (self.status.picture) { // 有配图
cellHeight = CGRectGetMaxY(self.pictureImageView.frame) + 10;
} else { // 无配图
cellHeight = CGRectGetMaxY(self.text_Label.frame) + 10;
}
return cellHeight;
}