// 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.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGStatusCell class]) bundle:nil] forCellReuseIdentifier:ID];
// 大概行高
self.tableView.estimatedRowHeight = 200;
}
#pragma mark - 數(shù)據(jù)源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.statuses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 傳遞模型數(shù)據(jù)
cell.status = self.statuses[indexPath.row];
return cell;
}
// 方案:在這個方法返回之前就要確定好cell的高度
XMGStatusCell *cell;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 如果沒有找到,那么就去storyboard里面找
}
cell.status = self.statuses[indexPath.row];
return cell.cellHeight;
}
@end
// 模型數(shù)據(jù)
#import <UIKit/UIKit.h>
// XMGStatus.h
#define XMGTextFont [UIFont systemFontOfSize:14]
#define XMGNameFont [UIFont systemFontOfSize:17]
@interface XMGStatus : NSObject
/** 圖像*/
@property (nonatomic ,copy) NSString *icon;
/** 昵稱*/
@property (nonatomic ,copy) NSString *name;
/** 內(nèi)容(正文)*/
@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
{
// 手動設(shè)置label的文字的最大寬度(目的:為了能夠計算label的高度,得到最真實(shí)的尺寸)
self.text_Label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}
// 設(shè)置數(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
{
// 強(qiáng)制布局(目的:讓label根據(jù)設(shè)置的約束計算自己最真實(shí)尺寸)
[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
xib圖解:
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。