cell的自适应高度

来源:互联网 发布:掉头发 白头发 知乎 编辑:程序博客网 时间:2024/05/04 19:26

设有三张图及三张段文字并且一一对应匹配,那么应该获取显示文本Label的尺寸和相应图片的UIImageView尺寸

需要注意的是无论图片的大小如何变化,但是图片的长宽之比是不变的



(1).求cell高度的方法是tableviewdelegate所提供的协议方法,主要是用来设置每一行高度该方法在MainViewController.m写

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    只有三张图片所以indexPath范围是0 - 2;

    根据图片的尺寸设置行高

    UIImage *image = [UIImageimageNamed:self.picArr[indexPath.row]];

    通过CGSize找到image里面图片的尺寸

   CGSize picSize = image.size;

    

    计算行高

    CGFloat rowHeight = picSize.height *WIDTH / picSize.width;

    图片的高度和文字的高度加起来是cell的高度

    根据对应的文字求出celllabel显示的高度

   NSDictionary *fontDic = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:14];                        NSFontAttributeName,nil];

    

    根据文字的大小,计算出文本的尺寸

    还需要指定一个尺寸(375, 0)( 0是最大值)

    第三个参数:计算高度需要依据字体的哪个特征来确定

    CGRect rect = [self.ziArr[indexPath.row]boundingRectWithSize:CGSizeMake(WIDTH,0)                                           options:NSStringDrawingUsesLineFragmentOriginattributes:fontDic context:nil];

    最后把结果作为返回值返回

    return rowHeight + rect.size.height;   

}


(2).自定义cell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        [selfcreateView];

    }

    returnself;

}

-(void)createView

{

    self.image1 = [[UIImageViewalloc]init];

    self.image1.backgroundColor = [UIColorcyanColor];

    [self.contentViewaddSubview:self.image1];

    [self.image1release];

    

    self.myLabel = [[UILabelalloc]init];

    self.myLabel.backgroundColor = [UIColorgrayColor];

    

    指定label的字体大小

    self.myLabel.font = [UIFontsystemFontOfSize:14];

    

    0是最大行数

    self.myLabel.numberOfLines =0;

    [self.myLabelsizeToFit];

    

    [self.contentViewaddSubview:self.myLabel];

    [self.myLabelrelease]; 

}


-(void)layoutSubviews

{

    [superlayoutSubviews];

    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 = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:14],NSFontAttributeName, nil];

    根据文字的大小,计算出文本的尺寸

    还需要指定一个尺寸(375, 0)// 0是最大值

    第三个参数:计算高度需要依据字体的哪个特征来确定

    CGRect rect = [self.myLabel.textboundingRectWithSize:CGSizeMake(375,0)                                                            options:NSStringDrawingUsesLineFragmentOriginattributes:fontDic context:nil];

    self.myLabel.frame =CGRectMake(0, height,self.contentView.frame.size.width, rect.size.height);

}

-(void)dealloc

{

    [self.image1release];

    [superdealloc];

}


使用方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    staticNSString *reuse = @"reuse";

    zidingyiTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:reuse];

    if (cell ==nil) {

        cell = [[[zidingyiTableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:reuse] autorelease];

    }

    cell.image1.image = [UIImageimageNamed:[NSStringstringWithFormat:@"%ld.png", indexPath.row +1]];

    cell.myLabel.text =self.ziArr[indexPath.row];

    return cell;

}









0 0
原创粉丝点击