Ios UITableViewCell调整大小以适应详细信息文本

Ios UITableViewCell调整大小以适应详细信息文本,ios,objective-c,uitableview,resize,Ios,Objective C,Uitableview,Resize,我正在尝试调整UITableViewCell的大小,以显示/隐藏所选内容的部分细节文本。 所有单元格显示详细信息文本的第一行。当用户点击单元格时,应显示整个细节文本,而不移动文本的当前可见部分 目前,我正在做以下工作: 在cellforrowatinexpath: cell.textLabel.text = ...; cell.detailTextLabel.text = ...; if ([indexPath isEqual:self.currentSelection]) { cel

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

目前,我正在做以下工作:
cellforrowatinexpath:

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

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

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

[tableView reloadData];
以及在
高度中的行路径

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
)会向上或向下跳跃几个像素。
如何防止这种情况并获得平滑的外观?

您可以这样做:-

-(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;
}

可以为详细信息文本(隐藏元素)添加高度约束,并将其导出。然后在隐藏该元素时设置其约束==0

我找到了解决这个问题的方法:
我没有使用
detailLabel
,而是创建了一个
NSAttributedString
,并使用了
textlab.attributedText
。属性字符串只是以比其他字符串更大的字体显示标题(以前在
textlab
中)(以前在
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;
}
如您所见,我手动限制了除所选单元格之外的所有单元格的文本长度

然后,在
cellforrowatinexpath
中,我将其分配如下:

cell.textLabel.attributedText = [self attributedTextForIndexPath:indexPath];
以及在索引路径的高度中:

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 

我该把它放在哪里?我还需要rowatindexpath的高度吗??不,你可以把这个放在CellForRowatinex-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{static NSString*CellIdentifier=@“CellIdentifier”;UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];if(cell==nil){cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];[cell.textLabel sizeToFit];}cell.textLable.text=@“;返回单元格;}感谢您的快速响应。我尝试过这样做,但现在它无法根据选择调整大小。