uitableview autolayout自定义单元格导致额外高度

时间:2017-07-13 09:53:14

标签: ios objective-c uitableview autolayout

uitableview autolayout自定义单元格导致额外高度。在不同的项目工作正常时,无法检测导致问题的位置。 enter image description here

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

    [_stubCell layoutSubviews];

    [self configureCell:_stubCell atIndexPath:indexPath];

    CGFloat height = [_stubCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return height;
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40.f;
}

- (void)configureCell:(CCACustomCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.customLabel.text = [_tableData objectAtIndex:indexPath.row];
    cell.customLabel.font = [UIFont fontWithName:@"Avenir-Light" size:16.0];
    cell.customLabel.textColor = UIColorFromRGB(0x32383B);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CCACustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

2 个答案:

答案 0 :(得分:0)

您是否可以提供有关如何创建_stubCell的详细信息?这可能是由于标签上的某些限制。 你试过计算字符串高度吗?

+(CGFloat )calculateStringHeight:(NSString *)string WithFont:(UIFont *)font andSidePadding:(CGFloat )padding{

  CGRect rect = [string boundingRectWithSize:CGSizeMake([[UIScreen mainScreen]bounds].size.width - (2*padding), 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
  return rect.size.height;
}

答案 1 :(得分:0)

您可以像这样返回标签所需的高度。您应该在LABEL_DESCRIPTION_WIDTH中传递您的描述标签宽度。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CGSize constrainedSize = CGSizeMake(LABEL_DESCRIPTION_WIDTH , 9999);
   NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:FONT_NAME size:FONT_SIZE], NSFontAttributeName,nil];
   NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[_tableData objectAtIndex:indexPath.row] attributes:attributesDictionary];
   CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
   return requiredHeight;
}