UITableView!別再用代碼計算行高了(一)
你還在用代碼去計算行高嗎?你不感覺那種方式很low嗎?從今天起,試著做些改變吧!
別給我講你喜歡寫代碼的感覺,你就是要用代碼去計算行高,那我這篇文章不適合你。
在講解復雜內容之前,還是先學習簡單的內容,本篇就是講解一些基本的內容。
一、純文字Cell
一般我們用的都是UILabel控件,這個控件配合Autolayout簡直是完美,廢話不多說。
我們首先創建一個簡單的工程,工程中我們創建一個UITableViewController子類,這裏命名為LabelViewController,下圖是一些初始配置
上面的第二步操作,我們也可以通過代碼設置rowHeight和estimatedRowHeight為UITableViewAutomaticDimension來達到目的
接下來我們要對cell做一些配置,我們只做一些簡單的約束設置就能達到目的,我們往cell的Content View上添加一個UILabel,設置約束如下圖:
好了,現在我們可以去寫代碼了,我們創建一個UITableViewCell的子類,命名為LabelCell(這裏是個人習慣,你也可以不創建),代碼如下:
#import <UIKit/UIKit.h>
@interface LabelCell : UITableViewCell
- (void)showText:(NSString *)text;
@end
#import "LabelCell.h" @interface LabelCell () @property (weak, nonatomic) IBOutlet UILabel *textLabel; @end @implementation LabelCell - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)showText:(NSString *)text { self.textLabel.text = text; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end
現在回到我們創建的LabelViewController類中,首先貼上代碼
@interface LabelViewController () @property (nonatomic, strong) NSArray *datas; @end @implementation LabelViewController - (void)viewDidLoad { [super viewDidLoad]; self.datas = @[@"中新網1月13日電 據教育部網站消息,教育部日前發布關於做好2018年春節寒假期間有關工作的通知,強調要紮實做好校園安全工作,要在放假前開學前分別開展一次校園安全大", @"《通知》要求,用心務實做好師生特別是困難師生走訪慰問工作。各級領導幹部要主動深入基層,深入教學一線,深入師生群眾,深入農村偏遠艱苦地區了解實際情況。要重點關心關註農村留守兒童、隨遷子女、經濟困難學生、殘疾學生等群體的學習生活情況,創造條件促進解決實際困難"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.datas.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { LabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LabelCell" forIndexPath:indexPath]; [cell showText:self.datas[indexPath.row]]; return cell; } @end
到此為止,我們已經完成了所有設置,運行程序看看
達到了我們的預期,不在去計算文字的高就能完美適配行高。
二、圖片Cell
我們在同一個工程中創建所需內容,在Main.storyboard中再拖入一個UITableViewController,同樣要設置對應的UITableView類的rowHeight和estimatedRowHeight為UITableViewAutomaticDimension,你可以選擇在storyboard(參考上面設置)中或者代碼設置。往Cell中拖入一個UIImageView,約束參考下圖:
其實這些設置已經達到了高度自適應,可我還是要給你接下來的代碼,創建一個ImageViewController類,和剛剛創建的UITableViewController綁定,再創建一個ImageCell類,和UITableViewController中的Cell綁定。
#import <UIKit/UIKit.h>
@interface ImageCell : UITableViewCell
- (void)showDataWithImageName:(NSString *)imageName;
@end
#import "ImageCell.h"
@interface ImageCell ()
@property (weak, nonatomic) IBOutlet UIImageView *testImageView;
@end
@implementation ImageCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)showDataWithImageName:(NSString *)imageName {
self.testImageView.image = [UIImage imageNamed:imageName];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
@interface ImageViewController ()
@property (nonatomic, strong) NSArray *datas;
@end
@implementation ImageViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.datas = @[@"test1", @"test2", @"test3"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.datas.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ImageCell" forIndexPath:indexPath];
[cell showDataWithImageName:self.datas[indexPath.row]];
return cell;
}
@end
運行程序,如果沒有錯誤的話,可以達到我們需要的自適應,看下圖:
以上總的來說就是將rowHeight和estimatedRowHeight設置為UITableViewAutomaticDimension,並且約束設置好,我們就不需要用代碼去計算行高。由於本篇不是講解約束的的文章,所以不會拉約束的人,你可以看看這:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/
後續我將發布更復雜的Cell自適應文章,感興趣的可以關註我,
以上工程代碼可以從這裏下載:https://gitee.com/yyxy/UITableViewDemo
UITableView!別再用代碼計算行高了(一)