0

由于某些奇怪的原因,TableView 单元格无法正确拟合动态文本内容。它们应该在单元格的顶部和底部之间留有少许边距。不幸的是,根据文本内容的数量,一些单元格没有顶部和底部边距(紧密包装),而另一些则有巨大的边距。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    float result = 30;
    if (indexPath.section != 0){

        CGSize size = [[self textForIndexPath:indexPath isTitle:NO] sizeWithFont:kITDescriptionFont constrainedToSize:CGSizeMake(220, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
        result = MAX(result, size.height);
    }

    return result;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *doubleCellIdentifier = @"ITDoubleDetailCEllIdentifier";
    static NSString *cellIdentifier = @"ITDetailCEllIdentifier";


    NSString *cellIdent = (indexPath.section == 0)? doubleCellIdentifier : cellIdentifier;
    ITDescriptionCell *cell = (ITDescriptionCell *)[tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (!cell) {
        UITableViewCellStyle style = (indexPath.section == 0)? UITableViewCellStyleValue2 : UITableViewCellStyleDefault;
        cell = [[ITDescriptionCell alloc] initWithStyle:style reuseIdentifier:cellIdent];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell.textLabel setNumberOfLines:0];
        [cell.textLabel setFont:kITDescriptionFont];
    }
    else {
        [cell.textLabel setText:@""];
        [cell.detailTextLabel setText:@""];
    }

    [cell sizeToFit];

    return cell;
}
4

1 回答 1

0

您需要动态计算文本的高度,并根据该高度和所需的填充,通过以下委托方法调整单元格的高度:

#define PADDING 10.0f
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Subscription *sub = [_dictTask valueForKeyPath:@"subscription"];

    NSArray *meta = sub.meta.allObjects;

    NSString *text = [[meta objectAtIndex:indexPath.row] valueForKey:@"sum_value"];
    CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];

    return textSize.height + PADDING * 3;
}
于 2013-06-09T03:44:36.640 回答