自定义的tableViewCell怎样固定imageView的大小位置

来源:互联网 发布:地下城网络中断啥毛病 编辑:程序博客网 时间:2024/05/23 07:24

我拉一个自定义cell时,下载的照片老是充满cell,自定义imageView的大小也不管用,因为图片的大小是无法固定的:

解决办法:

1、在本地找一张图片加载上去,在下载后替换;

2、重加载layoutSuperViews

(1)

@implementation More_ViewController

-(void)layoutSubviews{

   UIImage * img = self.imageView.image;

   self.imageView.image = [UIImageimageNamed:@"1.png"];

    [superlayoutSubviews];

    self.imageView.image = img;

   self.imageView.frame =CGRectMake(10,30, 200, 150);

}

@end

(2)网上的

很多页面其实就是这种展示结果,通常需要 imageView , textLabel , detailTextlabel , UITableViewCell本身提供了方便的自动布局(当有图片和没图片时,textLabeldetailLabel的位置会左右自动调整).但是图片的大小却是没有办法固定的(直接设置 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图片,等待重新布局完后再将原本的图片设置回去就可以了

0 0
原创粉丝点击