[自定義不等高的cell]-storyboard方式iOS8之前
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
@end
// ViewController.m
#import "ViewController.h"
#import "XMGStatusCell.h"
#import "XMGStatus.h"
#import "MJExtension.h"
@interface ViewController ()
/** 所有的微博模型*/
@property (nonatomic ,strong) NSArray *statuses;
@end
@implementation ViewController
- (NSArray *)statuses
{
if (!_statuses) {
_statuses = [XMGStatus mj_objectArrayWithFilename:@"statuses.plist"];
}
return _statuses;
}
NSString *ID = @"status";
- (void)viewDidLoad {
[super viewDidLoad];
// self-sizing(iOS8 以后)
// // 告訴tableView所有cell的高度是自動計算的(根據(jù)設置的約束來計算)
// self.tableView.rowHeight = UITableViewAutomaticDimension;
//
// // 告訴tableView所有cell的估算高度(減少tableView:heightForRowAtIndexPath:..的調用次數(shù))
self.tableView.estimatedRowHeight = 200;
// 不把底部@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureBottomLc;約束除掉,到時候傳數(shù)據(jù),根據(jù)約束計算的子控件的frame不對
}
#pragma mark - 數(shù)據(jù)源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.statuses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(@"cellForRowAtIndexPath--%zd",indexPath.row);
XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 傳遞模型數(shù)據(jù)
cell.status = self.statuses[indexPath.row];
return cell;
}
XMGStatusCell *cell;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(@"heightForRowAtIndexPath--%zd",indexPath.row);
// 創(chuàng)建一個臨時的cell(目的:為了傳遞indexPath這一行對應的數(shù)據(jù),布局內部所有的子控件,得到子控件的frame,進而能夠計算cell的高度)
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:ID];
}
// 傳遞模型數(shù)據(jù)
cell.status = self.statuses[indexPath.row];
return cell.cellHeight;
}
@end
// 模型數(shù)據(jù)
// XMGStatus.h
#import <UIKit/UIKit.h>
#define XMGTextFont [UIFont systemFontOfSize:14]
#define XMGNameFont [UIFont systemFontOfSize:17]
@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;
@end
// XMGStatus.m
#import "XMGStatus.h"
@implementation XMGStatus
@end
// XMGStatusCell.h
#import <UIKit/UIKit.h>
@class XMGStatus;
@interface XMGStatusCell : UITableViewCell
/** 微博模型*/
@property (nonatomic ,strong) XMGStatus *status;
/** cell的高度*/
- (CGFloat)cellHeight;
@end
// XMGStatusCell.m
#import "XMGStatusCell.h"
#import "XMGStatus.h"
@interface XMGStatusCell ()
/** 圖像*/
@property (nonatomic ,weak)IBOutlet UIImageView *iconImageView;
/** 昵稱*/
@property (nonatomic ,weak)IBOutlet UILabel *nameLabel;
/** vip*/
@property (nonatomic ,weak)IBOutlet UIImageView *vipImageView;
/** 正文*/
@property (nonatomic ,weak)IBOutlet UILabel *text_Label;
/** 配圖*/
@property (nonatomic ,weak)IBOutlet UIImageView *pictureImageView;
@end
@implementation XMGStatusCell
- (void)awakeFromNib
{
// 手動設置label的文字的最大寬度(目的:為了能夠計算label的高度,得到最真實的尺寸)
self.text_Label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}
// 設置數(shù)據(jù)
- (void)setStatus:(XMGStatus *)status
{
_status = status;
self.iconImageView.image = [UIImage imageNamed:status.icon];
self.nameLabel.text = status.name;
if (status.isVip) { // 是VIP
self.vipImageView.hidden = NO;
self.nameLabel.textColor = [UIColor orangeColor];
} else {
self.vipImageView.hidden = YES;
self.nameLabel.textColor = [UIColor blackColor];
}
self.text_Label.text = status.text;
if (status.picture) { // 有配圖
self.pictureImageView.hidden = NO;
self.pictureImageView.image = [UIImage imageNamed:status.picture];
} else {
self.pictureImageView.hidden = YES;
}
}
- (CGFloat)cellHeight
{
// 強制布局(目的:讓label根據(jù)設置的約束計算自己最真實尺寸)
[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;
}
@end
- 筆者認真想了一天,怎么透徹的理解全局這個cell的作用,
- 最終筆者想到一個恰當?shù)睦?,就比如說,把cell比作一個水杯,容器
- 里面的數(shù)據(jù)status就是水,算每一個cell的高度,就是算每一次倒入水的高度
iOS8之前要刪除的約束 圖片:
約束效果圖片:
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。