• 首先需要创建一个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. Java项目集成SAP BO

    SAP BO报表查看需要登录SAP BO系统,为了方便公司希望将BO报表集成到OA系统中,所以参考网上资料加上与SAP BO的顾问咨询整理出一套通过Java来集成SAP BO的功能. SAPBO中的报 ...

  2. vue 自定义组件

    1.Vue.component('component-test', { props:{}, data:function(){ return{} }, mounted:function(){}, com ...

  3. CentOS的改变系统启动级别

    CentOS7改变系统启动级别   systemctl命令:   文本模式:systemctl set-default multi-user.target 图形模式:systemctl set-def ...

  4. iOS App内存优化之 解决UIImagePickerController的图片对象占用RAM过高问题

    这个坑会在特定的情况下特别明显: 类似朋友圈的添加多张本地选择\拍照 的图片 并在界面上做一个预览功能 由于没有特别的相机\相册需求,则直接使用系统自带的UIImagePickerController ...

  5. MySQL学习笔记(二):MySQL数据类型汇总及选择参考

    本文主要介绍了MySQL 的常用数据类型,以及实际应用时如何选择合适的类型.  ******几个通用的简单原则:******* 1. 更小的通常更好.但是要确保没有低估需要存储的值的范围,如果无法确定 ...

  6. Ztree _ 横向显示子节点、点击文字勾选、去除指定元素input的勾选状态

    前些天项目需要树结构表现数据,需求ztree就能满足所以直接使用ztree只是踩了些小坑... 1.ztree子节点横向显示(下图): 效果说明:第三级子节点按需求横向显示其他竖向显示,每行最多显示5 ...

  7. python socket.error: [Errno 10054] 解决方法

    我用的是python2.7   我搜网上10054错误解决方法的时候发现,大部分文章都是以python3为基础的,对于python2不适用. python socket.error: [Errno 1 ...

  8. 简单工厂设计模式--Java设计模式(一)

    一 概念: 简单工厂模式就是通过一个工厂类根据参数动态创建对应的类. 二 案例 我们以汽车作为例子,在现实生活中汽车只是一个抽象的产品,其中有很多类型的汽车才是具体产品,如奔驰.宝马.保时捷等等(当然 ...

  9. 64位linux系统通过编译安装apache+…

    二.安装php 上传php压缩包 例如:php-5.2.3.tar.gz 移动 mv php-5.2.3.tar.gz /usr/local/src 进入 cd /usr/local/src 解压 t ...

  10. 16. leetcode 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example:     3    / \   9  20     /  \    15 ...