一聚教程网:一个值得你收藏的教程网站

热门教程

iOS UITableViewCell自适应高度的例子

时间:2022-06-25 23:37:29 编辑:袖梨 来源:一聚教程网

例如淘宝购买完商品后的评价,评价过的评价列表里,每个人评价的内容不同,评价内容有多有少,我们一般都是用UITableView来创建界面的,这时候就需要cell自适应高度了。代码示例:

 代码如下 复制代码

EvaluateTableViewCell.h

#import
 
@interface EvaluateTableViewCell : UITableViewCell
@property (nonatomic,strong) UILabel *phoneLabel;
@property (nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) UILabel *descLabel;
@property (nonatomic,strong) UIView *intervalView;
 
//评价内容并且实现自动换行
-(void)setIntroductionText:(NSString*)text;
 
@end


EvaluateTableViewCell.m

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //用户手机号
        self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(7, 15, 100, 12)];
        self.phoneLabel.font = FONT(12);
        [self addSubview:self.phoneLabel];
        //评价时间
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(247 * (kScreenWidth / 320), 15, 72, 11)];
        self.timeLabel.font = FONT(11);
        self.timeLabel.textColor = [UIColor grayColor];
        [self addSubview:self.timeLabel];
        //分隔线
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(8, self.timeLabel.maxY + 14, kScreenWidth - 16, 1)];
        lineView.backgroundColor = [UIColor blackColor];
        [self addSubview:lineView];
        //评价内容
        self.descLabel = [[UILabel alloc] initWithFrame:CGRectMake(11, lineView.maxY+ 29, kScreenWidth - 22, 13)];
        self.descLabel.font = FONT(12);
        self.descLabel.numberOfLines = 0;
        [self addSubview:self.descLabel];
        //间隔
        self.intervalView = [[UIView alloc] initWithFrame:CGRectMake(0, self.descLabel.maxY + 9, kScreenWidth, 2)];
        self.intervalView.backgroundColor = [UIColor blackColor];
        [self addSubview:self.intervalView];
    }
    return self;
}
//赋值 and 自动换行,计算出cell的高度
-(void)setIntroductionText:(NSString*)text{
    //获得当前cell高度
    CGRect frame = [self frame];
    //文本赋值
    self.descLabel.text = text;
    //设置label的最大行数
    CGSize size = CGSizeMake(300, 1000);
    CGSize labelSize = [self.descLabel.text sizeWithFont:self.descLabel.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];
    self.descLabel.frame = CGRectMake(self.descLabel.frame.origin.x, self.descLabel.frame.origin.y, labelSize.width, labelSize.height);
    
    //计算出自适应的高度
    frame.size.height = labelSize.height + 85;
    self.frame = frame;
    self.intervalView.frame = CGRectMake(0, self.descLabel.maxY + 9, kScreenWidth, 4);
}


 

cellzishiyingViewController.m

 代码如下 复制代码


#import "cellzishiyingViewController.h"
#import "EvaluateTableViewCell.h"
 
@interface cellzishiyingViewController (){
    UITableView *evaluateTableView;
    UILabel *evaluateLabel;
    UILabel *goodEvaluateLabel;
}
 
@end
 
@implementation cellzishiyingViewController
 
 
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB(242, 242, 247);
    self.automaticallyAdjustsScrollViewInsets = NO;
    evaluateTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kHeaderHeight, kScreenWidth, kScreenHeight - kHeaderHeight) style:UITableViewStylePlain];
    evaluateTableView.delegate = self;
    evaluateTableView.dataSource = self;
    evaluateTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:evaluateTableView];
}
#pragma mark - 行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
#pragma mark - 行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    return 95;
    EvaluateTableViewCell *cell = [self tableView:evaluateTableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}
#pragma mark - 每行内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indefier = @"cell";
    EvaluateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indefier];
    if (!cell) {
        cell = [[EvaluateTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indefier];
    }
    NSArray *oneArray = @[@"18374637823",@"18643535325",@"15653543746",@"13924366238"];
    NSArray *twoArray = @[@"2016-04-04",@"2016-04-06",@"2016-05-04",@"2016-09-04"];
    NSArray *threeArray = @[@"由于iOS是遵循MVC模式设计的,很多操作都是通过代理和外界沟通的,但对于数据源控件除了代理还有一个数据源属性,通过它和外界进行数据交互。",@"UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped。这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显示而已",@"由于iOS是遵循MVC模式设计的,很多操作都是通过代理和外界沟通的,但对于数据源控件除了代理还有一个数据源属性,通过它和外界进行数据交互。 对于UITableView设置完dataSource后需要实现UITableViewDataSource协议,在这个协议中定义了多种 数据操作方法",@"切实开展批评和自我批评,勇于揭露和纠正工作中"];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.phoneLabel.text = oneArray[indexPath.row];
    cell.timeLabel.text =  twoArray[indexPath.row];
    cell.descLabel.text = threeArray[indexPath.row];
    [cell setIntroductionText:cell.descLabel.text];
    return cell;
}

注:

 代码如下 复制代码


// 当前屏幕宽度
#define kScreenWidth    [UIScreen mainScreen].bounds.size.width
// 当前屏幕高度
#define kScreenHeight   [UIScreen mainScreen].bounds.size.height

热门栏目