iOS中Cell高度如何实现自动适应内容

1.先创建

CustomTableViewCell.h 和 CustomTableViewCell.m (注意该类继承于: UIViewController)

在CustomTableViewCell.h添加以下属性和方法

//

//  CustomTableViewCell.h

//  Cell高度自适应

//

//  Created by rimi on 15/9/2.

//  Copyright (c) 2015 gaoqingbiao. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface CustomTableViewCell : UITableViewCell

@property (nonatomic ,strong) UILabel *userName;            //用户名

@property (nonatomic ,strong) UILabel *interduction;    // 用户简介

@property (nonatomic ,strong) UIImageView *userImage;   // 用户头像

- (void)setInterductionTextHeight:(NSString *)text;

@end

CustomTableViewCell.m  中的实现如下

//

//  CustomTableViewCell.m

//  Cell高度自适应

//

//  Created by rimi on 15/9/2.

//  Copyright (c) 2015 gaoqingbiao. All rights reserved.

//


#import "CustomTableViewCell.h"


@implementation CustomTableViewCell


// 重写父类的 initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifie的方法

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

{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        // 添加视图

        [self.contentView addSubview:self.userName];

        [self.contentView addSubview:self.interduction];

        [self.contentView addSubview:self.userImage];

    }

    return self;


}

/**

 *  方法返回interduction(用户介绍文本)的宽度

 *

 *  @param text 用户所介绍的文字

 */

- (void)setInterductionTextHeight:(NSString *)text

{

    // 获取当前cell的高度

    CGRect frame = [self frame];

    // 文本赋值

    self.interduction.text = text;

    // 设置文本自动换行

    self.interduction.numberOfLines = 10;

    [self.interduction sizeToFit];

    

    CGSize size = CGSizeMake(300, 1000);

    

    CGSize labelSize = [self.interduction.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_interduction.font} context:nil].size;

    frame.size.height = labelSize.height + 100;

    self.frame = frame;

}

/**

 *  userName ----getter

 *  懒加载

 *  @return 返回用户名的labele

 */

- (UILabel *)userName

{

    if (!_userName) {

        _userName = [[UILabel alloc] initWithFrame:CGRectMake(90, 20, 30, 20)];

        _userName.backgroundColor = [UIColor redColor];

    }

    return _userName;

}

/**

 *  interduction ---getter

 *  懒加载

 *  @return 返回用书介绍的label

 */

- (UILabel *)interduction

{

    if (!_interduction) {

        _interduction =  [[UILabel alloc] initWithFrame:CGRectMake(20, 90, 350, 40)];

        _interduction.backgroundColor = [UIColor blueColor];

     

    }

    return _interduction;

}

/**

 *  userImage ---getter

 *  懒加载

 *  @return 返回用户头像

 */

- (UIImageView *)userImage

{

    if (!_userImage) {

        _userImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];

        _userImage.layer.cornerRadius = 40;

        _userImage.backgroundColor = [UIColor yellowColor];

    }

    return _userImage;

}


@end

至此自定义cell已经书写完成接下来只需将其添加到视图中去即可

2.创建

TableViewController.h 和 TableViewController.m文件 

这里TableViewController.h文件中不做任何改变只需在TableViewController.m文件中做实现具体实现方式如下:

//

//  TableViewController.m

//  Cell高度自适应

//

//  Created by rimi on 15/9/2.

//  Copyright (c) 2015 gaoqingbiao. All rights reserved.

//


#import "TableViewController.h"

#import "CustomTableViewCell.h"

@interface TableViewController ()<UITableViewDelegate,UITableViewDataSource>{

    NSMutableArray *_tableDate;         // 存放数据

}



@property (nonatomic,strong) UITableView *tableView;

@end


@implementation TableViewController


// 视图加载

- (void)viewDidLoad {

    [super viewDidLoad];

    // 添加表格视图

    [self.view addSubview:self.tableView];

}



// 设置单元格 组数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


// 设置每组所包含的单元格行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 8;

}

// 很据每一个cell返回每一行的高度

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

{

    // 获取生成的行

    UITableViewCell *cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath];

    // 返回该行的高度

    return cell.frame.size.height;

}

// 自定义单元格的使用

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

{

    // 单元格重用

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];

    if (!cell) {

        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"identifier"];

    }

    // 添加数据

    if (indexPath.row == 0) {

        [cell setInterductionTextHeight:@"我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!"];

    } else if (indexPath.row == 2){

        [cell setInterductionTextHeight:@"我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!"];

    } else {

        [cell setInterductionTextHeight:@"我是多啦A梦我有肚子,我是多啦A梦我有肚子!!"];

    }

    return cell;

}

/**

 *  tableeView getter

 *

 *  @return 返回表格视图

 */

-(UITableView *)tableView

{

    if (!_tableView) {

        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];

        // 设置代理

        _tableView.delegate = self;

        _tableView.dataSource = self;

    }

    return _tableView;

}


@end


书写完以上内容就算大功告成。效果就是这样:



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值