UITableView可以算是使用频率最高的组件之一的,在开发过程中经常需要展示一些简单的信息列表
常见列表布局

如图,很多页面其实就是这种展示结果,通常需要imageView,textLabel,detailTextlabel,而UITableViewCell本身提供了方便的自动布局(当有图片和没图片时,textLabel和detailLabel的位置会左右自动调整). 但是图片的大小却是没有办法固定的(直接设置imageView.frame是无法固定imageView的大小的),那么一般来说解决这个问题的办法有两种:

  • 固定显示图片的大小(包括PlaceHolder)
  • 自定义tableViewCell,添加自定义的imageView,textLabeldetailTextLabel

这两种方式都可以解决这个问题,但是这两种方式其实都挺麻烦的,能否直接固定imageView的大小呢? 方法是有的,只需要重载layoutSubviews即可

派生UITableViewCell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//自定义一个Cell
@interface MMCell : UITableViewCell @end @implementation MMCell //重载layoutSubviews
- (void)layoutSubviews
{
UIImage *img = self.imageView.image;
self.imageView.image = [UIImage imageName:@"res/PlaceHolder.png"];
[super layoutSubviews];
self.imageView.image = img;
} @end

这样,我们只要使用MMCell就可以固定imageView的大小了,且大小为PlaceHolder.png的大小(一般来说这种页面都会使用一个PlaceHolder.png来显示默认图片).

原理是在UItableVeiwlayoutSubviews调用时,会根据imageView.image的大小来调整imageView,textLabel,detailTextLabel的位置,在此之前我们先将imageView.image设置为PlaceHolder.png图片,等待重新布局完后再将原本的图片设置回去就可以了

如何让 UITableViewCell 中的 imageView 大小固定的更多相关文章

  1. iOS UITableViewCell 中 调整imageView 的图片大小

    在我的项目中,很多地方都希望将UITableViewCell 中的imageView 能根据自己图片的大小来进行展示,而就为了解决这个问题又觉得重写UITableViewCell 很不值得. 如下: ...

  2. UITableVIewcell中图片不能改变大小的原因

    你有没没有发现,有时候把图片放进cell.imageView中时无法顺利改变大小呢? 其实根本原因是要在layoutSubviews重新配置一下,cell的布局里面默认有一个imageiView,同时 ...

  3. 基于BaseAdapter的GridView设置ImageView大小

    基于BaseAdapter的GridView设置ImageView大小 网上找了好多,都是基于SimpleAdapter的,本身在Item.xml中就对ImageView设置了id,而基于BaseAd ...

  4. UITableview自定义accessory按钮和ImageView大小一致

    if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseI ...

  5. Java ByteArrayOutputStream中buf 的大小增长问题

    问题:写入固定长度的字符串[write(byte b[])],观察ByteArrayOutputStream中buf 的大小始终比字符串 Bytes的Size大很多,很是不解 分析发现: privat ...

  6. Android中的ImageView的scaleType属性详解

    ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等. 设置的方式包括: 1. 在layout xml中定义android:s ...

  7. 【Swift】UITableViewCell 中 TTTAttributedLabel 超链接无法点击的问题

    前言 还以为是自己代码写的有问题,用法和别的地方都一样,但是这个是在 UITableViewCell 中使用,另外在 tableHeaderView 中使用也没用这个问题 —— 使用 TTTAttri ...

  8. 如何得到自定义UITableViewCell中的按钮所在的cell的indexPath.row

    在自定义UITableViewCell中创建了一个按钮. 想在点击该按钮时知道该按钮所在的cell在TableView中的行数.就是cell的 indexPath.row两种方法都很好.-(IBAct ...

  9. ListView 中的ImageView Button

    1.ListView 中的ImageView  Button的点击事件会屏蔽掉ItemView的点击事件 原因是ImageView Button在回执过程中会抢夺item的焦点 解决办法:在xml中添 ...

随机推荐

  1. 事务复制中的snapshot

        Snapshot agent读取article的信息,将article的内容和脚本放置到snapshot文件夹中: 接下来distribution agent会读取这些快照文件,传输到订阅,完 ...

  2. Membership三步曲

    http://www.cnblogs.com/jesse2013/p/membership-part1.html http://www.cnblogs.com/jesse2013/p/membersh ...

  3. Only top uni produces good ppt.

    重要的事说三遍: 学技术,我只相信那几所top高校 学技术,我只相信那几所top高校 学技术,我只相信那几所top高校 <Good Habits you must own> Wake up ...

  4. iOS-多线程-GCD

    一. 名词解释: 1. 进程和线程 进程是指在系统中正在运行的一个应用程序.每个进程之间都是独立的,每个进程均运行在期专用而且受到保护的内存空间中. 线程是指一个进程想要执行任务,就必须要有线程.线程 ...

  5. iOS-开发技巧-页面布局

    #pragma mark - Life Cycle//1.初始化//2.view did load//3.view will appear…#pragma mark - System Delegate ...

  6. LeetCode——Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  7. Direct3D11学习:(七)绘图基础——彩色立方体的绘制

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 在前面的几篇文章中,我们详细介绍了Direct3D渲染所需要的数学基础和渲染管道理论知识.从这篇文章开始,我们就 ...

  8. ok6410 android driver(10)

    From this essay, we go to a new discussion "Android Hardware Abstraction Layer". In this e ...

  9. XmlNodeList循环读取节点值

    foreach (XmlNode item in XmlNodeList) { string oid = item.SelectSingleNode("oid").InnerTex ...

  10. Android入门:Activity四种启动模式

    一.启动模式介绍 启动模式简单地说就是Activity启动时的策略,在AndroidManifest.xml中的标签的android:launchMode属性设置: 启动模式有4种,分别为standa ...