// 創建自定義cell添加子控件的方法initWithStyle(note:子控件要添加到contentView上) - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier NS_AVAILABLE_IOS(3_0); // 傳統創建自定義view添加子空間的方法 //- (instancetype)initWithFrame:(CGRect)frame // 自定義xib時調用的方法 //- (void)awakeFromNib; //- (instancetype)initWithCoder:(NSCoder *)coder // 布局子控件 - (void)layoutSubviews { [super layoutSubviews]; } //設置數據 - (void)setXX:(模型數據類型 *)XX
Masonry
//除掉前綴 #define MAS_SHORTHAND //可接收數據類型參數 #define MAS_SHORTHAND_GLOBALS #import "Masonry.h" - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { // 常用的間距 CGFloat margin = 10; CGFloat contentViewW = CGRectGetWidth(self.contentView.frame); CGFloat contentViewH = CGRectGetHeight(self.contentView.frame); // 1.圖片 UIImageView *icon_ImageView = [[UIImageView alloc] init]; [self.contentView addSubview:icon_ImageView]; //icon_ImageView.backgroundColor = [UIColor blueColor]; self.icon_ImageView = icon_ImageView; [icon_ImageView makeConstraints:^(MASConstraintMaker *make) {// make.left.equalTo(self.contentView.left).offset(margin);// make.top.equalTo(self.contentView.top).offset(margin); make.top.left.equalTo(self.contentView).offset(margin); make.bottom.equalTo(self.contentView.bottom).offset(-margin); make.width.equalTo(80); }];}
自定義不等高cell
// 添加子控件(把有可能顯示的子控件都加進去) - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier //布局子空間Frame - (void)layoutSubviews { [super layoutSubviews]; } // 設置子控件顯示的數據 - (void)setXX:(模型數據類型 *)XX //方案1:在heightForRowAtIndexPath:方法調用之前將所有cell的高度計算清楚 /** * 返回每一行cell的具體高度 */- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ JXStatus *status = self.statuses[indexPath.row]; CGFloat margin = 10; CGFloat cellHeight = 0; // 頭像 CGFloat iconX = margin; CGFloat iconY = margin; CGFloat iconWH = 30; CGRect iconImageViewFrame = CGRectMake(iconX, iconY, iconWH, iconWH); // 文字 CGFloat textX = iconX; CGFloat textY = CGRectGetMaxY(iconImageViewFrame) + margin; CGFloat textW = [UIScreen mainScreen].bounds.size.width - 2 * textX; CGSize textMaxSize = CGSizeMake(textW, MAXFLOAT); NSDictionary *textAttrs = @{NSFontAttributeName : [UIFont systemFontOfSize:14]}; CGFloat textH = [status.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttrs context:nil].size.height; CGRect text_labelFrame = CGRectMake(textX, textY, textW, textH); // 配圖 if (status.picture) { CGFloat pictureWH = 100; CGFloat pictureX = textX; CGFloat pictureY = CGRectGetMaxY(text_labelFrame) + margin; CGRect pictureImageViewFrame = CGRectMake(pictureX, pictureY, pictureWH, pictureWH); cellHeight = CGRectGetMaxY(pictureImageViewFrame); } else { cellHeight = CGRectGetMaxY(text_labelFrame); } cellHeight += margin; return cellHeight;}// 方案2:在模型中計算cell高度,返回高度直接從模型中取出- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ JXStatus *status = self.statuses[indexPath.row]; return status.cellHeight;}//模型數據#import <UIKit/UIKit.h>@interface JXStatus : NSObject/**** 文字/圖片數據 ****//** 姓名 */@PRoperty (nonatomic, copy) NSString *name;/** 文本 */@property (nonatomic, copy) NSString *text;/** 頭像 */@property (nonatomic, copy) NSString *icon;/** 配圖 */@property (nonatomic, copy) NSString *picture;/** 是否為會員 */@property (nonatomic, assign) BOOL vip;/**** frame數據 ****//** 頭像的frame */@property (nonatomic, assign) CGRect iconFrame;/** 昵稱的frame */@property (nonatomic, assign) CGRect nameFrame;/** 會員的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#import "JXStatus.h"@implementation JXStatus- (CGFloat)cellHeight{ if (_cellHeight == 0) { CGFloat margin = 10; // 頭像 CGFloat iconX = margin; CGFloat iconY = margin; CGFloat iconWH = 30; self.iconFrame = CGRectMake(iconX, iconY, iconWH, iconWH); // 昵稱(姓名) CGFloat nameY = iconY; CGFloat nameX = CGRectGetMaxX(self.iconFrame) + margin; // 計算文字所占據的尺寸 NSDictionary *nameAttrs = @{NSFontAttributeName : [UIFont systemFontOfSize:17]}; CGSize nameSize = [self.name sizeWithAttributes:nameAttrs]; self.nameFrame = (CGRect){{nameX, nameY}, nameSize}; // 會員圖標 if (self.vip) { CGFloat vipW = 14; CGFloat vipH = nameSize.height; CGFloat vipY = nameY; CGFloat vipX = CGRectGetMaxX(self.nameFrame) + margin; self.vipFrame = CGRectMake(vipX, vipY, vipW, vipH); } // 文字 CGFloat textX = iconX; CGFloat textY = CGRectGetMaxY(self.iconFrame) + margin; CGFloat textW = [UIScreen mainScreen].bounds.size.width - 2 * textX; CGSize textMaxSize = CGSizeMake(textW, MAXFLOAT); NSDictionary *textAttrs = @{NSFontAttributeName : [UIFont systemFontOfSize:14]}; CGFloat textH = [self.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttrs context:nil].size.height; self.textFrame = CGRectMake(textX, textY, textW, textH); // 配圖 if (self.picture) { CGFloat pictureWH = 100; CGFloat pictureX = textX; CGFloat pictureY = CGRectGetMaxY(self.textFrame) + margin; self.pictureFrame = CGRectMake(pictureX, pictureY, pictureWH, pictureWH); _cellHeight = CGRectGetMaxY(self.pictureFrame); } else { _cellHeight = CGRectGetMaxY(self.textFrame); } _cellHeight += margin; } return _cellHeight;}@end
// 告訴tableView所有cell的真實高度是自動計算(根據設置的約束來計算)self.tableView.rowHeight = UITableViewAutomaticDimension;// 告訴tableView所有cell的估算高度self.tableView.estimatedRowHeight = 44;
新聞熱點
疑難解答