cell自适应高度

cell自适应高度

import “MyCell.h”

define WIDTH self.contentView.frame.size.width

define HEIGHT self.contentView.frame.size.height

@implementation MyCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createData];
}
return self;
}

-(void)createData{
self.picImageView=[[UIImageView alloc] init];
[self.contentView addSubview:self.picImageView];
// self.oneImageView.backgroundColor=[UIColor greenColor];
[self.picImageView release];

self.myLabel=[[UILabel alloc] init];
[self.contentView addSubview:self.myLabel];
self.myLabel.textColor=[UIColor redColor];

// self.myLabel.textAlignment=NSTextAlignmentCenter;

// 指定label的字体大小,默认字体是17号
self.myLabel.font=[UIFont systemFontOfSize:14];
self.myLabel.numberOfLines=0;
[self.myLabel sizeToFit];

[self.myLabel release];

}

-(void)layoutSubviews{
[super layoutSubviews];
// 让imageView的尺寸和cell的图片大小相同
// 因为这个方法是最后一个被执行的,所以执行到这个方法的时候,应经对cell的各个属性进行完赋值操作,所以可以通过imageView.image找到图片的尺寸
CGSize picSize=self.picImageView.image.size;
CGFloat height=WIDTH*picSize.height/picSize.width;
self.picImageView.frame=CGRectMake(0, 0, WIDTH, height);

NSMutableDictionary *fontDic=[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
CGRect rect=[self.myLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
self.myLabel.frame=CGRectMake(0, height, WIDTH, rect.size.height);

}

  • (void)dealloc
    {
    [_myLabel release];
    [_picImageView release];
    [super dealloc];
    }

import “MainViewController.h”

import “MyCell.h”

define WIDTH self.view.frame.size.width

define HEIGHT self.view.frame.size.height

@interface MainViewController ()

pragma mark 这个方法是tableView的delegate所提供的协议方法,主要是用来设置每行高度

  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{
    // 根据图片的尺寸,设置行高
    UIImage *image=[UIImage imageNamed:self.picArr[indexPath.row]];
    // 通过CGSize找到image里面图片的尺寸
    CGSize picSize=image.size;
    // 计算行高
    CGFloat rowHeight=picSize.height*WIDTH/picSize.width;

    // 计算label的高度
    // 根据对应的文字求出cell上label显示的高度
    NSMutableDictionary *fontDic=[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    // 根据文字的大小,计算出文本的尺寸
    // 还需要制定一个尺寸(375,0)0是高度最高的
    // 第三个参数:计算高度需要依据字体的哪个特征来确定
    CGRect rect=[self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];

    // 最后把结果作为返回结果
    return rowHeight+rect.size.height;
    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.picArr.count;
    }

  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse=@”reuse”;
    MyCell *cell=[tableView dequeueReusableHeaderFooterViewWithIdentifier:reuse];
    if (!cell) {
    cell=[[[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
    }
    cell.picImageView.image=[UIImage imageNamed:self.picArr[indexPath.row]];
    cell.myLabel.text=self.ziArr[indexPath.row];
    return cell;
    }

  • (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath{

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值