星空网 > 软件开发 > 操作系统

如何让 UITableViewCell 中的 imageView 大小固定

UITableView可以算是使用频率最高的组件之一的,在开发过程中经常需要展示一些简单的信息列表
如何让 UITableViewCell 中的 imageView 大小固定images/loading.gif' data-original="http://7xibqv.com1.z0.glb.clouddn.com/2013-12-06%2013.33.25.png" />常见列表布局

如图,很多页面其实就是这种展示结果,通常需要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 大小固定

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

外国媒体:https://www.goluckyvip.com/tag/376.html
物流葵花:https://www.goluckyvip.com/tag/3760.html
流量红利:https://www.goluckyvip.com/tag/37600.html
流量说:https://www.goluckyvip.com/tag/37601.html
流量运营中心 内部专用 :https://www.goluckyvip.com/tag/37602.html
流量中心社群:https://www.goluckyvip.com/tag/37603.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流