自定義非等高cell

非等高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];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市联逻,隨后出現(xiàn)的幾起案子档痪,更是在濱河造成了極大的恐慌岖常,老刑警劉巖额获,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異播演,居然都是意外死亡蹬挤,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門调鲸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來盛杰,“玉大人,你說我怎么就攤上這事藐石〖垂” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵于微,是天一觀的道長逗嫡。 經常有香客問我青自,道長,這世上最難降的妖魔是什么祸穷? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任性穿,我火速辦了婚禮,結果婚禮上雷滚,老公的妹妹穿的比我還像新娘需曾。我一直安慰自己,他們只是感情好祈远,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布呆万。 她就那樣靜靜地躺著,像睡著了一般车份。 火紅的嫁衣襯著肌膚如雪谋减。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天扫沼,我揣著相機與錄音出爹,去河邊找鬼。 笑死缎除,一個胖子當著我的面吹牛严就,可吹牛的內容都是我干的。 我是一名探鬼主播器罐,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼梢为,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了轰坊?” 一聲冷哼從身側響起铸董,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎肴沫,沒想到半個月后粟害,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡樊零,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年我磁,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片驻襟。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡夺艰,死狀恐怖,靈堂內的尸體忽然破棺而出沉衣,到底是詐尸還是另有隱情郁副,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布豌习,位于F島的核電站存谎,受9級特大地震影響拔疚,放射性物質發(fā)生泄漏。R本人自食惡果不足惜既荚,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一稚失、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧恰聘,春花似錦句各、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至兼蕊,卻和暖如春初厚,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背孙技。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工产禾, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人牵啦。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓下愈,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蕾久。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內容