• 首先需要创建一个model类,继承于NSObject;
  • 定义两个属性:

@property(nonatomic,retain)NSString *imageName;

@property(nonatomic,retain)NSString *imageDescription;

  • 然后我们需要在ViewController里边创建tableView,并且遵守它的协议,实现两个必须遵守的代理方法。
 UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor orangeColor];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.modelArray.count;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Model *model = self.modelArray[indexPath.row]; static NSString *modelIdentifier = @"model";
ModelCell *cell = [tableView dequeueReusableCellWithIdentifier:modelIdentifier];
if (nil == cell) {
cell = [[ModelCell alloc ] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:modelIdentifier];
}
cell.model = model; return cell;
}
  • 然后我们需要有一个方法来修改cell的高度:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//取出当前的model
Model *model = self.modelArray[indexPath.row];
CGSize imageSize = [UIImage imageNamed:model.imageName].size;
CGFloat scale = tableView.bounds.size.width / imageSize.width;
CGFloat imageViewHeight = imageSize.height * scale;
//计算尺寸
CGRect rect = [model.imageDescription boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, 1000.f) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:20.f]} context:nil]; return imageViewHeight + rect.size.height; }
  • 下边是我们自定义cell的代码:
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.modelImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
self.modelImageView.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.modelImageView]; self.modelImageDescriptionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.modelImageDescriptionLabel.backgroundColor = [UIColor greenColor];
self.modelImageDescriptionLabel.font = [UIFont systemFontOfSize:20.f];
//设置label的默认显示文字的行数,0为自动;
self.modelImageDescriptionLabel.numberOfLines = 0;
[self.contentView addSubview:self.modelImageDescriptionLabel]; }
return self; }
//重写setter方法。接收model,
-(void)setModel:(Model *)model {
if (_model != model) {
_model = model;
self.modelImageView.image = [UIImage imageNamed:model.imageName];
self.modelImageDescriptionLabel.text = model.imageDescription;
}
} -(void)layoutSubviews {
[super layoutSubviews];
//首先拿到原始图片的尺寸;
CGSize imageSize = self.modelImageView.image.size;
//求比例(宽和宽的比等于高和高的比)
CGFloat scale = self.contentView.bounds.size.width / imageSize.width;
self.modelImageView.frame = CGRectMake(0, 0, self.contentView.bounds.size.width,imageSize.height * scale); //字符串的计算文本高度的方法,
//参数1:文本计算需要给定的尺寸
//参数2:计算选项:(第一个是以行为单位去计算,第二个是参考字体去计算)
//参数3:字符串的属性(是字典类型的,来设置字体的大小)
//参数4:绘制上下文,填nil就可以,
CGRect rect = [self.model.imageDescription boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width, 1000.f) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20.f]} context:nil]; self.modelImageDescriptionLabel.frame = CGRectMake(0, self.modelImageView.frame.origin.y + self.modelImageView.frame.size.height, self.contentView.frame.size.width, rect.size.height); }

Cell的更多相关文章

  1. iOS开发之多种Cell高度自适应实现方案的UI流畅度分析

    本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...

  2. 使用Autolayout实现UITableView的Cell动态布局和高度动态改变

    本文翻译自:stackoverflow 有人在stackoverflow上问了一个问题: 1 如何在UITableViewCell中使用Autolayout来实现Cell的内容和子视图自动计算行高,并 ...

  3. iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法

    "UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...

  4. UICollectionView布局cell的三种方式

    UICollectionViewFlowLayout里面: // 方法一 - (void)prepareLayout{} // 方法二 - (nullable NSArray<__kindof ...

  5. Cell右滑 多个编辑选项栏

    简单粗暴,一看就能明白 关于右滑cell,能滑出来两个以上的选项栏,可以如下这么做,但是要注意下面的注意事项,就是关于iOS8前后的问题,注释写的很清楚了.可以直接复制到自己的代码里看的会更明白. / ...

  6. UICollectionViewCell--查找cell上的按钮点击后,对应的是哪个cell

    实际写项目会碰到各种各样的问题,废话不多说 按钮添加到cell时,根据是直接添加到self还是self.contentView上,在点击方法里找到btn的父视图 我是直接添加到self上,所以只有一层 ...

  7. UITableView cell复用出错问题 页面滑动卡顿问题 & 各杂七杂八问题

    UITableView 的cell 复用机制节省了内存,但是有时对于多变的自定义cell,重用时会出现界面出错(例如复用出错,出现cell混乱重影).滑动卡顿等问题,这里只简单敲下几点复用出错时的解决 ...

  8. UITableView点击每个Cell,Cell的子内容的收放

    关于点击TableviewCell的子内容收放问题,拿到它的第一个思路就是, 方法一: 运用UITableview本身的代理来处理相应的展开收起: 1.代理:- (void)tableView:(UI ...

  9. 【Swift】iOS UICollectionView 计算 Cell 大小的陷阱

    前言 API 不熟悉导致的问题,想当然的去理解果然会出问题,这里记录一下 UICollectionView 使用问题. 声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cn ...

  10. [iOS]技巧集锦:UITableView自定义Cell中的控件无法完全对齐Cell的左边界和右边界

    这是个很诡异的问题,由于一些特殊需求,我的TableView的Cell的背景色是透明,其中的控件会有背景色,第一个控件和最后一个控件我都用IB自动设了约束,对齐Cell的左边界和右边界,但是自动约束很 ...

随机推荐

  1. 设计模式(3)抽象工厂模式(Abstract Factory)

    设计模式(0)简单工厂模式 设计模式(1)单例模式(Singleton) 设计模式(2)工厂方法模式(Factory Method) 源码地址 0 抽象工厂模式简介 0.0 抽象工厂模式定义 抽象工厂 ...

  2. Selenium自动化初级/中级网络授课班招生

    近期学习selenium和appium的测试人员越来越多,应广大刚接触UI自动化以及对selenium想要更深入了解的测试人员的要求,特请一位资深测试架构师为我们开课讲解selenium,以及如何设计 ...

  3. C语言基础 - 实现单向链表

    回归C基础 实现一个单向链表,并有逆序功能 (大学数据结构经常是这么入门的) //定义单链表结构体 typedef struct Node{ int value; struct Node *next; ...

  4. 【PHP】 安装参数

    1. 配置参数 './configure' '--prefix=/usr/local/php5.2' '--with-apxs2=/usr/sbin/apxs' '--with-mysql=/usr/ ...

  5. 打造 高性能,轻量级的 webform框架---js直接调后台(第二天)

    问题2: 每次与后台打交道 都需要写一些自己都看不太懂的事件,而且传参数很麻烦,这就是.net 封装的事件,如何解决呢?        首先以为webfrom事件,都需要写 服务器控件来绑定后台的事件 ...

  6. HTML基础知识(未完待续)

    一.HTML编辑工具:Sublime Text 二.HTML实体字符:1.( 空格):&nbsp: 2.(<) &lt: 3.(>)&gt: 4.(&)&a ...

  7. YII2 添加全局自定义函数

    方法一: 这种方法就是直接在入口文件web/index.php里面写函数,示例代码如下: 全局函数 function pr($var){ //do something } (new yii\web\A ...

  8. mysql导出指定字段或指定数据到文件中

    使用mysqldump把mysql数据库的数据导出到文件中还是挺方便的:比如说要导出baijunyao数据库: // mysqldump -u用户名 -p 数据库名 [表名]> 导出的文件名 m ...

  9. 仿PC版微信的练手项目(可实时通讯)

    仿PC版微信的DEMO 本项目是由一个仿PC版微信的vue前端项目,和一个使用leancloud进行数据存储的.提供WebSocket的node后端项目构成. 本项目使用的技术栈:vue + vue- ...

  10. Android Studio中添加SlidingMenu

    SlidingMenu是github上面的一个开源库,地址:https://github.com/jfeinstein10/SlidingMenu.git 第一步:先下载: 第二步:添加到as中: 1 ...