我拉一个自定义cell时,下载的照片老是充满cell,自定义imageView的大小也不管用,因为图片的大小是无法固定的:
解决办法:
1、在本地找一张图片加载上去,在下载后替换;
2、重加载layoutSuperViews
(1)
@implementation More_ViewController
-(void)layoutSubviews{
UIImage * img = self.imageView.image;
self.imageView.image = [UIImage imageNamed:@"1.png"];
[super layoutSubviews];
self.imageView.image = img;
self.imageView.frame = CGRectMake(10, 30, 200, 150);
}
@end
(2)网上的很多页面其实就是这种展示结果,通常需要 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 图片,等待重新布局完后再将原本的图片设置回去就可以了