UITableViewCell调整大小以适合详细文本

时间:2014-11-18 14:45:39

标签: ios objective-c uitableview resize

我正在尝试调整UITableViewCell的大小,以便在选择时显示/隐藏部分详细信息文本。 所有单元格都显示详细文本的第一行。当用户点击一个单元格时,应显示整个细节文本而不移动文本的当前可见部分。

目前,我正在做以下事项:
cellForRowAtIndexPath:

cell.textLabel.text = ...;
cell.detailTextLabel.text = ...;

if ([indexPath isEqual:self.currentSelection])
{
    cell.detailTextLabel.numberOfLines = CGFLOAT_MAX;
}
else
{
    cell.detailTextLabel.numberOfLines = 1;
}

didSelectRowAtIndexPath:

if ([self.currentSelection isEqual:indexPath])
{
    self.currentSelection = nil;
}
else
{
    self.currentSelection = indexPath;
}

[tableView reloadData];

并在heightForRowAtIndexPath

float minHeight = 60;

if ([indexPath isEqual:self.currentSelection])
{
    KPLexiconEntry *lexiconEntry = ...;

    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);

    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;

    NSDictionary * attributes = @{NSFontAttributeName : cellFont, NSParagraphStyleAttributeName : paragraphStyle};
    CGSize labelSize = [lexiconEntry.explanation boundingRectWithSize:constraintSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;

    float height = labelSize.height + 44;
    return height > minHeight ? height : minHeight;
}

return minHeight;

按照我的意愿显示或隐藏详细文本,但单元格内的文本(包括textLabel.text)会向上或向下跳几个像素。
如何防止这种情况并获得平滑的外观?

3 个答案:

答案 0 :(得分:0)

你能做到: -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifire = @"CellIdentifire";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifire];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifire];
      [cell.textLabel sizeToFit];
    }
    cell.textLable.text= @"";
    return cell;
}

答案 1 :(得分:0)

您可以为细节文本(隐藏元素)添加高度约束并将其输出。然后设置隐藏该元素时他的约束== 0;

答案 2 :(得分:0)

我找到了解决这个问题的方法:
我创建了一个detailLabel并使用了NSAttributedString,而不是使用textLabel.attributedText。属性字符串只显示标题(以前在textLabel中),其字体大于其余字符(以前称为detailLabel)。 我创建了一个创建属性字符串的方法:

- (NSAttributedString*)attributedTextForIndexPath:(NSIndexPath*)indexPath {
    id itemForIndexPath = ...;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] init];

    UIFont *titleFont = [UIFont systemFontOfSize:16];
    NSDictionary *titleAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: [UIColor clearColor], NSFontAttributeName: titleFont};
    NSString *titleRaw = [NSString stringWithFormat:@"%@\n", @"itemForIndexPath.title"];
    NSAttributedString *title = [[NSAttributedString alloc] initWithString:titleRaw attributes:titleAttributes];
    [attrString appendAttributedString:title];

    UIFont *textFont = [UIFont systemFontOfSize:14];
    NSDictionary *textAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: [UIColor clearColor], NSFontAttributeName: textFont};;
    NSString *textRaw = itemForIndexPath.text;
    if (![indexPath isEqual:self.currentSelection] && textRaw.length >= 30)
    {
        textRaw = [NSString stringWithFormat:@"%@...", [textRaw substringToIndex:27]];
    }

    NSAttributedString *text = [[NSAttributedString alloc] initWithString:textRaw attributes:textAttributes];
    [attrString appendAttributedString:text];

    return attrString;
}

如您所见,我手动限制除所选单元格以外的所有文本的长度。

然后,在cellForRowAtIndexPath中,我将其指定为:

cell.textLabel.attributedText = [self attributedTextForIndexPath:indexPath];

heightForRowAtIndexPath

NSAttributedString *text = [self attributedTextForIndexPath:indexPath];
CGSize constraintSize = CGSizeMake(255, CGFLOAT_MAX);
CGSize size = [text boundingRectWithSize:constraintSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
return ceil(size.height + 22); // adding 22 for padding