UITableViewCell高度自适应

UITableViewCell高度自适应
UITableView是iOS开发过程中常用的列表控件,固定高度的Cell布局再熟悉不过,比较麻烦的是高度不固定的问题,
比如用大量文字需要展示的。
一、手动计算,手动布局
主要的实现思路:计算好需要的高度,然后设置Cell高度和布局;
用的的代理方法的调用顺序
//1、先调用返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

//2、再调用返回Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;


关于计算高度的时间:1、在设置高度的时候计算 2、获取到数据初始化Model的时候计算;我选择后者,请求、解析数据、初始化model可以再一次等待过程解决,不需要再获取的时候现计算,速度要快些。创建Model如下:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface DataModel : NSObject

@property (nonatomic,copy)NSString *name;

@property (nonatomic,copy)NSString *text;

@property (nonatomic,assign)CGFloat rowHeight;

@end



#import "DataModel.h"

@implementation DataModel


-(void)setText:(NSString *)text
{
    _text = text;
    
    //315是文字控件的宽度,这里不涉及适配问题,直说解决思路
    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(315, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    self.rowHeight = rect.size.height;
}


@end
这里是在设置文字内容的时候计算高度。
自定义Cell的代码如下
#import <UIKit/UIKit.h>
#import "DataModel.h"

@interface CustomTableCell : UITableViewCell
//文字1
@property (strong, nonatomic)  UILabel *name;

//文字2
@property (strong, nonatomic)  UILabel *text;

//刷新
-(void)reload:(DataModel *)data;

@end



#import "CustomTableCell.h"

@implementation CustomTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        //添加两个UILabel
        self.name = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
        self.name.textAlignment = NSTextAlignmentCenter;
        self.name.backgroundColor = [UIColor grayColor];
        [self addSubview:self.name];
        
        self.text = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 315, 44)];
        self.text.backgroundColor = [UIColor lightGrayColor];
        self.text.numberOfLines = 0;
        [self addSubview:self.text];
    }
    return self;
}

-(void)reload:(DataModel *)data
{
    self.name.text = data.name;
    self.text.text = data.text;
    //刷新frame
    self.text.frame = CGRectMake(60, 0, 315, data.rowHeight);
}

@end

然后使用
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DataModel *data = [self.dataSource objectAtIndex:indexPath.row];
    return data.rowHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identi = @"Cell";
    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:Identi ];
    
    if (cell == nil) {
        cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identi];
    }
    
    DataModel *data = [self.dataSource objectAtIndex:indexPath.row];

    [cell reload:data];

    return cell;
}
效果如下图


二、手动计算,自动布局
计算过程同上,但Cell采取自动布局设置如下图



让Name保持距四方都是0,宽度为60;Text保持距四方都是0,numberOfLines为0(自动换行),然后使用
    [_tableView registerNib:[UINib nibWithNibName:@"CustomTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Cell"];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    DataModel *data = [self.dataSource objectAtIndex:indexPath.row];
    cell.name.text = data.name;
    cell.text.text = data.text;
    cell.text.backgroundColor = [UIColor yellowColor];
    return cell;
}
好处是方便做适配。


三、自动计算,自动布局
Cell的自动布局同上(二)中的截图,然后使得自动计算,关键性的API向下支持到iOS7
    _tableView.estimatedRowHeight = 44;
    _tableView.rowHeight = UITableViewAutomaticDimension;

然后具体实现代码如下
    [_tableView registerNib:[UINib nibWithNibName:@"CustomTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Cell"];
    _tableView.estimatedRowHeight = 44;
    _tableView.rowHeight = UITableViewAutomaticDimension;



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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    DataModel *data = [self.dataSource objectAtIndex:indexPath.row];
    cell.name.text = data.name;
    cell.text.text = data.text;

    return cell;
}
二、三的效果图.
总结:
从手动到自动,必然带来着开发效率的提升,直接对比方法一、三便能感受出来;但是把越多的工作交给系统的API来做,
便失去了响应的可自定义灵活性;比如 “方法一” 中可以获取整个Cell的高度和可以在内容较少时减小Tableview的高度
(如果效果需要),“方法三”便无法知道;其实“方法二”中庸有一的灵活性和三的方便性。文中代码Demo


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于UITableViewCell中包含WebView的自适应高度问题,一般的解决方案是在webViewDidFinishLoad方法中计算WebView的高度,并更新UITableViewCell高度。但是这种方法有时会出现计算不准确的情况,以及性能问题。 最新的解决方案是使用iOS 11中引入的UITableViewAutomaticDimension,结合约束自动布局来实现UITableViewCell自适应高度。具体步骤如下: 1. 在Storyboard或XIB中,设置UITableViewCell的约束,包括WebView的顶部、底部、左右两侧的约束,并将WebView的高度设置为大于等于0的值(可以是一个较小的值,比如10)。 2. 在tableView(_:cellForRowAt:)方法中,设置WebView的代理为当前的UITableViewCell,并在webView(_:didFinish:)方法中更新UITableViewCell高度: ```swift func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { // 计算WebView的内容高度 webView.evaluateJavaScript("document.readyState") { (result, error) in if result != nil { webView.evaluateJavaScript("document.body.offsetHeight", completionHandler: { (height, error) in if let height = height as? CGFloat { // 更新UITableViewCell高度 self.heightConstraint.constant = height self.setNeedsUpdateConstraints() self.updateConstraintsIfNeeded() self.layoutIfNeeded() self.delegate?.didUpdateHeight() } }) } } } ``` 3. 在tableView(_:estimatedHeightForRowAt:)方法中,返回一个估算的高度(可以是一个较小的值,比如100): ```swift func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return 100 } ``` 4. 在tableView(_:heightForRowAt:)方法中,返回UITableViewAutomaticDimension: ```swift func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } ``` 注意:在使用约束自动布局的情况下,需要保证UITableViewCell高度约束是完整的,即顶部和底部都有约束,否则自适应高度可能会出现问题。同时,在更新UITableViewCell高度时,需要调用setNeedsUpdateConstraints、updateConstraintsIfNeeded和layoutIfNeeded方法,以保证约束的更新及时生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值