设有三张图及三张段文字并且一一对应匹配,那么应该获取显示文本Label的尺寸和相应图片的UIImageView尺寸
需要注意的是无论图片的大小如何变化,但是图片的长宽之比是不变的
(1).求cell高度的方法是tableview的delegate所提供的协议方法,主要是用来设置每一行高度该方法在MainViewController.m写
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
只有三张图片所以indexPath范围是0 - 2;
根据图片的尺寸设置行高
UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]];
通过CGSize 找到image里面图片的尺寸
CGSize picSize = image.size;
计算行高
CGFloat rowHeight = picSize.height * WIDTH / picSize.width;
图片的高度和文字的高度加起来是cell的高度
根据对应的文字求出cell上label显示的高度
NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14]; NSFontAttributeName, nil];
根据文字的大小,计算出文本的尺寸
还需要指定一个尺寸(375, 0)( 0是最大值)
第三个参数:计算高度需要依据字体的哪个特征来确定
CGRect rect = [self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
最后把结果作为返回值返回
return rowHeight + rect.size.height;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createView];
}
return self;
}
-(void)createView
{
self.image1 = [[UIImageView alloc]init];
self.image1.backgroundColor = [UIColor cyanColor];
[self.contentView addSubview:self.image1];
[self.image1 release];
self.myLabel = [[UILabel alloc]init];
self.myLabel.backgroundColor = [UIColor grayColor];
指定label的字体大小
self.myLabel.font = [UIFont systemFontOfSize:14];
0是最大行数
self.myLabel.numberOfLines = 0;
[self.myLabel sizeToFit];
[self.contentView addSubview:self.myLabel];
[self.myLabel release];
}
-(void)layoutSubviews
{
[super layoutSubviews];
让imageView的尺寸和cell的图片大小相同
因为这个方法是在uihou一个被执行的,所以执行到这个方法的时候已经对cell的各个属性进行完整赋值操作,所以可以通过imageview,image找到图片的尺寸
CGSize picSize = self.image1.image.size;
CGFloat height = self.contentView.frame.size.width * picSize.height / picSize.width;
self.image1.frame = CGRectMake(0, 0, self.contentView.frame.size.width, height)
NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];
根据文字的大小,计算出文本的尺寸
还需要指定一个尺寸(375, 0)// 0是最大值
第三个参数:计算高度需要依据字体的哪个特征来确定
CGRect rect = [self.myLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
self.myLabel.frame = CGRectMake(0, height, self.contentView.frame.size.width, rect.size.height);
}
-(void)dealloc
{
[self.image1 release];
[super dealloc];
}
使用方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuse = @"reuse";
zidingyiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil) {
cell = [[[zidingyiTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
cell.image1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.png", indexPath.row + 1]];
cell.myLabel.text = self.ziArr[indexPath.row];
return cell;
}