• 首先需要创建一个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. poj_3461: Oulipo

    题目链接 基础KMP题,本文提供一段能AC并且便于调试以及查看next数组的代码. 参考博客 http://blog.csdn.net/v_july_v/article/details/7041827 ...

  2. nyoj_120: 校园网络

    题目链接 要加边使一个图成为一个强连通分量,只需加max(出度为0的点数,入度为0的点数)条边(可以不使用tarjan算法).题目数据有点弱,网上一些人所谓 结果 = 出度为0的点的数量+入度为0的点 ...

  3. Codeforces_499C:Crazy Town(计算几何)

    题目链接 给出点A(x1,y1),B(x2,y2),和n条直线(ai,bi,ci,aix + biy + ci = 0),求A到B穿过多少条直线 枚举每条直线判断A.B是否在该直线两侧即可 #incl ...

  4. new DefaultHttpClient过时处理建议和HTTP调用后关闭流处理

    因为工作中经常会写点接口类需求,写完HTTP的接口后,就要写测试类来调下服务端的代码.最近写新的测试调用代码时候,发现项目中new DefaultHttpClient()实例过期很久了,于是查阅了些资 ...

  5. (转)SqlServer基础之(触发器)(清晰易懂)

    阅读目录 一:触发器的优点 二:触发器的作用 三:触发器的分类 四:触发器的工作原理 五:创建触发器 六:管理触发器 概念:   触发器(trigger)是SQL server 提供给程序员和数据分析 ...

  6. HDOJ2008-数值统计

    Problem Description 统计给定的n个数中,负数.零和正数的个数.   Input 输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然 ...

  7. 初学 Python(十一)——切片

    初学 Python(十一)--切片 初学 Python,主要整理一些学习到的知识点,这次是切片. #-*- coding:utf-8 -*- ''''' 切片 ''' L = ['name','age ...

  8. 又见angular----步一步做一个angular4小项目

    这两天看了看angular4的文档,发现他和angular1.X的差别真的是太大了,官方给出的那个管理英雄的Demo是一个非常好的入门项目,这里给出一个管理个人计划的小项目,从头至尾一步一步讲解如何去 ...

  9. Fluent Validation with Web Api 2

    using FluentValidation;using FluentValidation.Attributes;using System;using System.Collections.Gener ...

  10. getComputedStyle方法获取元素CSS值

    javascript的style属性只能获取内联样式,对于外部样式和嵌入式样式需要用currentStyle属性.但是,currentStyle在FIrefox和Chrome下不支持,需要用getCo ...