UITableView 之 RowHeight

作者: 失忆的程序员 | 来源:发表于2021-06-24 09:50 被阅读0次
系统处理
/// tableview
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = RATIOA(44);
    }
    return _tableView;
}
//
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
///
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *cellIdentifier = @"nameCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.textLabel.text = @"-----";
        return cell;
}
 一下是 自己算高度  
/////
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [XPFStrNil x_getSizeForLabString:self.dataAry[indexPath.row] font:YGFONT(15) width:RATIOA(262)] + RATIOA(44);
}

#pragma mark ----- 计算文字的高度
+ (CGFloat)x_getSizeForLabString:(NSString *)labStr font:(UIFont *)fontInt width:(CGFloat)width
{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:labStr];
    NSRange allRange = [labStr rangeOfString:labStr];
    [attrStr addAttribute:NSFontAttributeName value:fontInt range:allRange];
    CGFloat titleHeight;
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading;
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:options context:nil];
    titleHeight = ceilf(rect.size.height);
    return titleHeight; //加两个像素,防止emoji被切掉.
}


+ (CGFloat)x_getSizeForLabString:(NSString *)labStr font:(UIFont *)fontInt height:(CGFloat)height
{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:labStr];
    NSRange allRange = [labStr rangeOfString:labStr];
    [attrStr addAttribute:NSFontAttributeName value:fontInt range:allRange];
    CGFloat titleWidth;
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading;
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:options context:nil];
    titleWidth = ceilf(rect.size.width);
    return titleWidth; //加两个像素,防止emoji被切掉.
}

           ************** 分割线 **************

新思路新方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
这就要求 你的cell , 
内部控件 头部top  和 底部bottom 要和cell 头部底部有明确的距离。
一定是bottom 要明确数,否则不好用。
不好用 加上这行代码,处理iOS 9/ iOS 10的系统。
_tableView.estimatedRowHeight = 44;


相关文章

网友评论

    本文标题:UITableView 之 RowHeight

    本文链接:https://www.haomeiwen.com/subject/obrwyltx.html