實(shí)用小技巧(七):UITableViewCell自適應(yīng)行高的計(jì)算

版本記錄

版本號 時(shí)間
V1.0 2017.06.25

前言

在app中到目前為止,我真沒找過沒有UITableView控件的app,可以說UITableView是app不可或缺的控件,在使用該控件的時(shí)候酷宵,不僅要知道數(shù)據(jù)源和代理方法及其他們的調(diào)用順序,還需要知道的是如何自適應(yīng)行高躬窜,只有自適應(yīng)行高才會(huì)優(yōu)化視覺浇垦。感興趣的可以看看我寫的其他小技巧
1. 實(shí)用小技巧(一):UIScrollView中上下左右滾動(dòng)方向的判斷

2. 實(shí)用小技巧(二):屏幕橫豎屏的判斷和相關(guān)邏輯
3.實(shí)用小技巧(三):點(diǎn)擊手勢屏蔽子視圖的響應(yīng)
4.實(shí)用小技巧(四):動(dòng)態(tài)的增刪標(biāo)簽視圖
5.實(shí)用小技巧(五):通過相冊或者相機(jī)更改圖標(biāo)
6.實(shí)用小技巧(六):打印ios里所有字體

一、只包含文字元素的cell

設(shè)計(jì)過程

先看一下文檔組織結(jié)構(gòu)荣挨。

文檔組織結(jié)構(gòu)

下面直接看代碼男韧。

1.JJTableViewVC.h

#import <UIKit/UIKit.h>

@interface JJTableViewVC : UIViewController

@end
2. JJTableViewVC.m
#import "JJTableViewVC.h"
#import "JJTableViewCell.h"

@interface JJTableViewVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray <NSString *> *dataListArr;

@end

@implementation JJTableViewVC

static NSString * const kJJTableViewCell = @"kJJTableViewCell";

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self loadData];
    
    [self setupUI];
}

#pragma mark - Object Private Function

- (void)setupUI
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[JJTableViewCell class] forCellReuseIdentifier:kJJTableViewCell];
    tableView.estimatedRowHeight = 100.0;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

- (void)loadData
{
    self.dataListArr = @[
                         @"我是第一個(gè)數(shù)據(jù),我是第一個(gè)數(shù)據(jù)",
                         @"我是第二個(gè)數(shù)據(jù)垦沉,我是第二個(gè)數(shù)據(jù)煌抒,我是第二個(gè)數(shù)據(jù)",
                         @"我是第三個(gè)數(shù)據(jù),我是第三個(gè)數(shù)據(jù)厕倍,我是第三個(gè)數(shù)據(jù)寡壮,我是第三個(gè)數(shù)據(jù),我是第三個(gè)數(shù)據(jù)",
                         @"我是第四個(gè)數(shù)據(jù),我是第四個(gè)數(shù)據(jù)况既,我是第四個(gè)數(shù)據(jù)这溅,我是第四個(gè)數(shù)據(jù),我是第四個(gè)數(shù)據(jù)棒仍,我是第四個(gè)數(shù)據(jù)悲靴,我是第四個(gè)數(shù)據(jù),我是第四個(gè)數(shù)據(jù)莫其,我是第四個(gè)數(shù)據(jù)癞尚,我是第四個(gè)數(shù)據(jù)"
                         ];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kJJTableViewCell];
    cell.titleStr = self.dataListArr[indexPath.row];
    return cell;
}

@end
3. JJTableViewCell.m
#import "JJTableViewCell.h"
#import "Masonry.h"

@interface JJTableViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;


@end

@implementation JJTableViewCell

#pragma mark - Override Base Function

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Object Private Function

- (void)setupUI
{
    //標(biāo)題
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = [UIColor blueColor];
    titleLabel.font = [UIFont systemFontOfSize:17.0];
    titleLabel.numberOfLines = 0;
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
}


- (void)layoutCellLayout
{
    //標(biāo)題
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15.0);
        make.top.equalTo(self.contentView).offset(15.0);
        make.bottom.equalTo(self.contentView).offset(-15.0);
        make.right.equalTo(self.contentView).offset(-15.0);
    }];
}

#pragma mark - Getter && Setter 

- (void)setTitleStr:(NSString *)titleStr
{
    _titleStr = titleStr;
    
    self.titleLabel.text = titleStr;
    
    [self layoutCellLayout];
}

@end

效果實(shí)現(xiàn)

下面看一下效果圖,如下所示乱陡。

效果圖

二浇揩、包含文字和圖片的cell

設(shè)計(jì)過程

先看一下代碼。

1. JJTableViewVC.m
#import "JJTableViewVC.h"
#import "JJTableViewCell.h"

@interface JJTableViewVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray <NSString *> *dataListArr;
@property (nonatomic, strong) NSArray <NSString *> *imageListArr;


@end

@implementation JJTableViewVC

static NSString * const kJJTableViewCell = @"kJJTableViewCell";

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self loadData];
    
    [self setupUI];
}

#pragma mark - Object Private Function

- (void)setupUI
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[JJTableViewCell class] forCellReuseIdentifier:kJJTableViewCell];
    tableView.estimatedRowHeight = 100.0;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

- (void)loadData
{
    self.dataListArr = @[
                         @"我是第一個(gè)數(shù)據(jù)憨颠,我是第一個(gè)數(shù)據(jù)",
                         @"我是第二個(gè)數(shù)據(jù)",
                         @"我是第三個(gè)數(shù)據(jù)胳徽,我是第三個(gè)數(shù)據(jù),我是第三個(gè)數(shù)據(jù)爽彤,我是第三個(gè)數(shù)據(jù)养盗,我是第三個(gè)數(shù)據(jù)",
                         @"我是第四個(gè)數(shù)據(jù),我是第四個(gè)數(shù)據(jù)适篙,我是第四個(gè)數(shù)據(jù)往核,我是第四個(gè)數(shù)據(jù),我是第四個(gè)數(shù)據(jù)匙瘪,我是第四個(gè)數(shù)據(jù)铆铆,我是第四個(gè)數(shù)據(jù)蝶缀,我是第四個(gè)數(shù)據(jù)丹喻,我是第四個(gè)數(shù)據(jù),我是第四個(gè)數(shù)據(jù),我是第四個(gè)數(shù)據(jù)翁都,我是第四個(gè)數(shù)據(jù)碍论,我是第四個(gè)數(shù)據(jù),我是第四個(gè)數(shù)據(jù)"
                         ];
    
    self.imageListArr = @[@"1", @"2", @"3", @"4"];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kJJTableViewCell];
    cell.titleStr = self.dataListArr[indexPath.row];
    cell.imageNameStr = self.imageListArr[indexPath.row];
    return cell;
}

@end
2. JJTableViewCell.m
#import "JJTableViewCell.h"
#import "Masonry.h"

@interface JJTableViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *contentImageView;



@end

@implementation JJTableViewCell

#pragma mark - Override Base Function

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Object Private Function

- (void)setupUI
{
    //圖片
    UIImageView *contentImageView = [[UIImageView alloc] init];
    contentImageView.image = [UIImage imageNamed:@"1"];
    [self.contentView addSubview:contentImageView];
    self.contentImageView = contentImageView;
    
    //標(biāo)題
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = [UIColor blueColor];
    titleLabel.font = [UIFont systemFontOfSize:17.0];
    titleLabel.numberOfLines = 0;
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
}


- (void)layoutUI
{
    //圖片計(jì)算后的告訴
    CGFloat pictureWidth = self.contentImageView.image.size.width;
    CGFloat pictureHeight = self.contentImageView.image.size.height;
    CGFloat fixWidth = 100.0;
    CGFloat imageHeight = pictureHeight / pictureWidth * fixWidth;
    
    //title的高度計(jì)算
    CGFloat titleWidth = self.bounds.size.width - 100 - 45.0;
    CGFloat titleHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(titleWidth, MAXFLOAT)].height;
    
    if (imageHeight > titleHeight) {
        
        //圖標(biāo)
        [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.contentView).offset(15.0);
            make.width.equalTo(@100.0);
            make.height.equalTo(@(imageHeight));
            make.bottom.equalTo(self.contentView).offset(-15.0);
        }];
        
        //標(biāo)題
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentImageView.mas_right).offset(15.0);
            make.top.equalTo(self.contentImageView);
            make.right.equalTo(self.contentView).offset(-15.0);
        }];
    }
    else {
        //圖標(biāo)
        [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.contentView).offset(15.0);
            make.width.equalTo(@100.0);
            make.height.equalTo(@(imageHeight));
        }];
        
        //標(biāo)題
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentImageView.mas_right).offset(15.0);
            make.top.equalTo(self.contentImageView);
            make.right.equalTo(self.contentView).offset(-15.0);
            make.bottom.equalTo(self.contentView).offset(-15.0);
        }];
    }
}

#pragma mark - Getter && Setter 

- (void)setTitleStr:(NSString *)titleStr
{
    _titleStr = titleStr;
    
    self.titleLabel.text = titleStr;
    
}

- (void)setImageNameStr:(NSString *)imageNameStr
{
    _imageNameStr = imageNameStr;
    
    self.contentImageView.image = [UIImage imageNamed:imageNameStr];
    
    [self layoutUI];
}

@end

效果實(shí)現(xiàn)

下面看一下效果實(shí)現(xiàn)圖柄慰,如下所示鳍悠。

實(shí)現(xiàn)效果圖

后記

未完,待續(xù)~~

伊萬卡
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末坐搔,一起剝皮案震驚了整個(gè)濱河市藏研,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌概行,老刑警劉巖蠢挡,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡业踏,警方通過查閱死者的電腦和手機(jī)禽炬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來勤家,“玉大人腹尖,你說我怎么就攤上這事》ゲ保” “怎么了热幔?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長讼庇。 經(jīng)常有香客問我断凶,道長,這世上最難降的妖魔是什么巫俺? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任认烁,我火速辦了婚禮,結(jié)果婚禮上介汹,老公的妹妹穿的比我還像新娘却嗡。我一直安慰自己,他們只是感情好嘹承,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布窗价。 她就那樣靜靜地躺著,像睡著了一般叹卷。 火紅的嫁衣襯著肌膚如雪撼港。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天骤竹,我揣著相機(jī)與錄音帝牡,去河邊找鬼。 笑死蒙揣,一個(gè)胖子當(dāng)著我的面吹牛靶溜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播懒震,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼罩息,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了个扰?” 一聲冷哼從身側(cè)響起瓷炮,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎递宅,沒想到半個(gè)月后娘香,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體冬筒,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年茅主,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了舞痰。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡诀姚,死狀恐怖响牛,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情赫段,我是刑警寧澤呀打,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站糯笙,受9級特大地震影響贬丛,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜给涕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一豺憔、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧够庙,春花似錦恭应、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至剔难,卻和暖如春胆屿,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背偶宫。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工非迹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人读宙。 一個(gè)月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓彻秆,卻偏偏與公主長得像楔绞,于是被迫代替她去往敵國和親结闸。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容