IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    如何让 UITableViewCell 中的 imageView 大小固定

    键盘上的舞者发表于 2016-03-14 02:26:32
    love 0

    一般来说有两种办法来解决这个问题:

    1.固定图片大小(包括placeholder)

    2.自定义tableViewCell,添加自定义的imageView

    但是现在有一种更简单的方法去解决这个问题。

    //自定义一个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

    原理:当UITableView的LayoutSubviews调用时,会根据imageView.image的大小来调整imageView,textLabel,detailTextLabel的位置,在此之前我们已经将imageView的image设置为placeHolder了,等待重新布局完之后再替换image就可以了,并且重新设置的图片大小会和placeHolder的尺寸一样。



沪ICP备19023445号-2号
友情链接