非等高cell創(chuàng)建方式1:xib
1.創(chuàng)建自定義類,繼承自UITableViewCell,勾選同時創(chuàng)建xib,創(chuàng)建完畢后設置cell的重用標識.
2.新建一個模型類,繼承自NSObject給cell提供數(shù)據(jù)源
3.將storyboard中的控制器及viewController文件刪掉,在storyboard中拖入一個UITableViewController,自定義一個WeiboStatusController類,將storyboard中控制器的class改為WeiboStatusController.
此時工程中包含了如下文件:
4.修改模型文件,將plist文件包含的屬性定義為成員屬性,寫入,并提供構造方法,方便外界創(chuàng)建模型數(shù)據(jù).
模型.h文件
#import <UIKit/UIKit.h>
@interface DLStatus : NSObject
//定義plist文件中的keys
@property(nonatomic,strong) NSString *icon;
@property(nonatomic,strong) NSString *name;
@property(nonatomic,strong) NSString *text;
@property(nonatomic,strong) NSString *picture;
@property(nonatomic,assign,getter=isVip) BOOL vip;
//定義cell的行高
@property(nonatomic,assign) CGFloat cellHeight;
//構造方法,方便外界創(chuàng)建模型數(shù)據(jù)
+ (instancetype)statusWithDict:(NSDictionary *)dict;
@end
模型.m文件如下:
#import "DLStatus.h"
@implementation DLStatus
+ (instancetype)statusWithDict:(NSDictionary *)dict{
DLStatus *status = [[self alloc] init];
//KVC(Key Value Coding)
[status setValuesForKeysWithDictionary:dict];
return status;
}
@end
5.把xib的控件與自定義cell連線,拿到對應的屬性.在.h中定義一個模型屬性,為cell傳遞模型數(shù)據(jù).同時提供構造方法,方便外界創(chuàng)建該類型的cell.
自定義類的.h文件內容如下:
#import <UIKit/UIKit.h>
@class DLStatus;
@interface DLStatusCell : UITableViewCell
@property(nonatomic,strong) DLStatus *status;
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end
自定義類的.m文件內容如下:
#import "DLStatusCell.h"
#import "DLStatus.h"
@interface DLStatusCell ()
@property (weak, nonatomic) IBOutlet UIImageView *name;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *vip;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIImageView *picture;
@end
@implementation DLStatusCell
//構造方法的實現(xiàn)
+ (instancetype)cellWithTableView:(UITableView *)tableView{
static NSString *ID = @"status";//將cell的創(chuàng)建封裝到類內部
DLStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {//緩存池找不到cell時,從xib創(chuàng)建
cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DLStatusCell class]) owner:nil options:nil] lastObject];
}
return cell;
}
- (void)awakeFromNib{
//設置contentLabel的最大寬度,防止layoutIfNeeded方法計算的尺寸偏差.
self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}
//重寫模型屬性的set方法
- (void)setStatus:(DLStatus *)status{
_status = status;
//設置模型屬性內部子控件的值
self.nameLabel.text = status.name;
//判斷vip的狀態(tài)
if (_status.isVip) {
self.vip.hidden = NO;
self.nameLabel.textColor = [UIColor orangeColor];
}else{
self.vip.hidden = YES;
self.nameLabel.textColor = [UIColor blackColor];
}
self.name.image = [UIImage imageNamed:status.icon];
self.contentLabel.text = status.text;
//判斷picture的狀態(tài)
if (_status.picture) {
self.picture.hidden = NO;
self.picture.image = [UIImage imageNamed:status.picture];
}else{
self.picture.hidden = YES;
}
//強制布局控件
[self layoutIfNeeded];
//返回cell的高度
if (self.picture.hidden) {
status.cellHeight = CGRectGetMaxY(self.contentLabel.frame) + 10;
}else{
status.cellHeight = CGRectGetMaxY(self.picture.frame) + 10;
}
}
@end
6.在WeiboStatusController文件中定義一個數(shù)組接收模型數(shù)據(jù),同時實現(xiàn)cell的數(shù)據(jù)源方法.
WeiboStatusController.m文件內容如下:
#import "WeiboStatusController.h"
#import "DLStatus.h"
#import "DLStatusCell.h"
@interface WeiboStatusController ()
//定義屬性,接收模型
@property (nonatomic,strong) NSArray *status;
@end
@implementation WeiboStatusController
//懶加載模型
- (NSArray *)status{
if (_status == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
NSArray *dictArr = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *statusArr = [NSMutableArray array];
for (NSDictionary *dict in dictArr) {
DLStatus *status = [DLStatus statusWithDict:dict];
[statusArr addObject:status];
}
_status = statusArr;
}
return _status;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - Table view data source
//返回cell的個數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.status.count;
}
//返回cell的內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//通過自定義cell創(chuàng)建cell
DLStatusCell *cell = [DLStatusCell cellWithTableView:tableView];
//取出對應的模型數(shù)據(jù)
cell.status = self.status[indexPath.row];
return cell;
}
//設置cell的估計高度,調換heightForRowAtIndexPath:與cellForRowAtIndexPath:的調用順序,同時優(yōu)化系統(tǒng)
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200;
}
//返回cell的真是高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//取出模型中對應的高度數(shù)據(jù)
DLStatus *status = self.status[indexPath.row];
return status.cellHeight;
}
@end
非等高cell創(chuàng)建方式2:storyboard
此方法創(chuàng)建cell只需要將xib文件中的子控件換到storyboard中,拖線,修改重用標識,將cell的構造方法改為如下內容即可:
+ (instancetype)cellWithTableView:(UITableView *)tableView{
return [tableView dequeueReusableCellWithIdentifier:ID];
}