你还在用代码去计算行高吗?你不感觉那种方式很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!别再用代码计算行高了(一)的更多相关文章

  1. IOS第八天(5:UITableViewController新浪微博, 计算行高)

    在 4 的 基础上重写 以下的方法 control #pragma mark - 代理方法 /** 计算单元格行高 */ - (CGFloat)tableView:(UITableView *)tab ...

  2. UITableView+FDTemplateLayoutCell计算行高显示<二>

    之前记录过一篇UITableView+FDTemplateLayoutCell计算行高不成功的博客... 传送门:http://www.cnblogs.com/pengsi/p/6571311.htm ...

  3. iOS 根据字符串内容动态计算行高

    + (CGFloat)textHeightFromTextString:(NSString *)text width:(CGFloat)textWidth fontSize:(CGFloat)size ...

  4. 一个简单的代码计算行数demo编写

    最近手头的项目基本上已经完结,历经了5个月的开发和迭代,各种的需求调整,想对自己的代码量进行一个客观的计算,于是抽了点时间写下了这个小demo,朋友们有需要的可以看看,很简单. 基本的思想就是:根目录 ...

  5. 动态的计算行高 加载数据源 有多少显示多少 tableView 包含 colloctionView 显示复杂的界面写法

    有时候,我们经常碰到这样的需求 先遵守代理 @interface PublishCollectionCell ()<UICollectionViewDataSource, UICollectio ...

  6. lable计算行高

    _introduce.text=status.introduce; //设置行间距 NSMutableAttributedString *attributedString = [[NSMutableA ...

  7. 对tableView三种计算动态行高方法的分析

    tableView是一个神奇的东西,可以这么说,就算是一个初学者如果能把tableView玩的很6,那编一般的iOS的需求都问题不大了.tableView是日常开发中用烂了的控件,但是关于tableV ...

  8. UITableView、UICollectionView行高/尺寸自适应

    UITableView 我们都知道UITableView从iOS 8开始实现行高的自适应相对比较简单,首先必须设置estimatedRowHeight给出预估高度,设置rowHeight为UITabl ...

  9. iOS开发——UI进阶篇(三)自定义不等高cell,如何拿到cell的行高,自动计算cell高度,(有配图,无配图)微博案例

    一.纯代码自定义不等高cell 废话不多说,直接来看下面这个例子先来看下微博的最终效果 首先创建一个继承UITableViewController的控制器@interface ViewControll ...

随机推荐

  1. VUE之随笔小总结1

    VUE 它是一个构建用户界面的JavaScript框架vue指令: 是带有v-前缀的特殊属性,通过属性来操作元素 v-text:在元素当中插入文本 eg:属性值会覆盖自己插入的值 //插入一段文本&l ...

  2. Solr学习笔记2(V7.2)---导入自己的数据

    学而不思则罔,思而不学则殆,总是看文档不动手效果是不好的.没有实地的从自己的数据库获取数据测试一下始终是空,总结一下自己的操作步骤吧. 第一步准备配置文件 E:\Solr\server\solr\co ...

  3. angularjs+ionic+'h5+'实现二维码扫描功能

    今天给大家分享一下基于angularjs与ionic框架实现手机二维码扫描的功能.没有用到cordova等任何插件,h5+实现的. 开发工具:hbuilder 首先,需要在hbuilder项目下面的配 ...

  4. TP5.0 excel 导入导出

    引第三方的phpexcel类库放到 ThinkPHP\Library\Vendor\demo下,自己建的文件夹demo 再将Excel.class放到ThinkPHP\Library\Org\clas ...

  5. shareInstance

    2.+(id)shareInstance; 外界初始化得到单例类对象的唯一借口,这个类方法返回的就是instance,即类的一个对象, 如果instance为空,则实例化一个对象,如果不为空,则直接返 ...

  6. CSS基础--常用样式

    一.背景相关 背景颜色 background-color :颜色名称/rgb值/十六进制值 背景图片 background-image :url('') 背景图片平铺方式 background-rep ...

  7. Intellij-工程目录下隐藏不想显示的文件和文件夹

    File-->Editor-->File Types

  8. junit设计模式--组合模式

    Composite,英语翻译下,复合,组合. 组合模式有时候又叫做部分-整体模式,它使我们在树形结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户 ...

  9. Apache Shiro java安全框架

    什么是Apache Shiro? Apache Shiro(发音为“shee-roh”,日语“堡垒(Castle)”的意思)是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理功能,可为 ...

  10. 理解Object.defineProperty的作用

    对象是由多个名/值对组成的无序的集合.对象中每个属性对应任意类型的值.定义对象可以使用构造函数或字面量的形式: var obj = new Object; //obj = {} obj.name = ...