UITableView!别再用代码计算行高了(一)

mac2022-06-30  19

你还在用代码去计算行高吗?你不感觉那种方式很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

转载于:https://www.cnblogs.com/Devhwl/p/8280408.html

相关资源:UITableView(cell)行高——根据文件内容自动设置
最新回复(0)