如何让 UITableViewCell 中的 imageView 大小固定

2025-05-06 16:37:46
推荐回答(1个)
回答1:

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

  固定显示图片的大小(包括PlaceHolder)
  自定义tableViewCell,添加自定义的 imageView , textLabel 和 detailTextLabel
  这两种方式都可以解决这个问题,但是这两种方式其实都挺麻烦的,能否直接固定imageView的大小呢? 方法是有的,只需要重载 layoutSubviews 即可

  派生UITableViewCell

  //自定义一个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 来显示默认图片).

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