Objective c 目标c-在运行时扩展单元格高度

Objective c 目标c-在运行时扩展单元格高度,objective-c,uitableview,heightforrowatindexpath,Objective C,Uitableview,Heightforrowatindexpath,我有一个自定义单元格,其中一个标签可以大到足以支持添加“查看更多”按钮。所以我把标签的内容减少到5行,然后添加这个按钮,如果需要的话,可以扩展内容 我的问题是:这可以在不使用tableView的重载数据的情况下完成吗 我在“查看更多”按钮(在自定义单元格内)的触摸事件中尝试了此代码: 什么也没发生!我想我需要一种方法来扩展单元格行高度和表视图的内容高度 提前感谢, 版本:我在链接中检查了解决方案,我已将此代码应用于自定义单元格(在触摸事件中): 我现在有两个问题: 1) reloadRowsA

我有一个自定义单元格,其中一个标签可以大到足以支持添加“查看更多”按钮。所以我把标签的内容减少到5行,然后添加这个按钮,如果需要的话,可以扩展内容

我的问题是:这可以在不使用tableView的重载数据的情况下完成吗

我在“查看更多”按钮(在自定义单元格内)的触摸事件中尝试了此代码:

什么也没发生!我想我需要一种方法来扩展单元格行高度和表视图的内容高度

提前感谢,


版本:我在链接中检查了解决方案,我已将此代码应用于自定义单元格(在触摸事件中):

我现在有两个问题: 1) reloadRowsAtIndexPaths正在执行整个重新加载数据。 2) 自定义单元格是知道单元格高度的单元格。使用此方法时,self.contentExpanded始终等于NO

***parentTableView方法取自


第二版:我认为问题不在高度上。我无法避免对所有单元格调用此方法

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath
 {        
    CellObject *object;  
      if(self.searchTableData)
          object = [self.searchTableData objectAtIndex:indexPath.row];
      else
          object = [self.database.cellObjectsFetchedResultsController objectAtIndexPath:indexPath];  


    id cell = [TableViewCellFactory createFromCellObject:object fromPrototypeTable:self.tableView];

    CGFloat height = [cell getHeightRowForCellObject:object];

    return height;

}
在自定义单元格中:

  self.contentView.frame = self.backgroundView.frame = self.accessoryView.frame =  CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height + 80);
  - (CGFloat)getHeightRowForCellObject:(CellObject *)cellObject
  {

CGFloat messageHeight = 0;

if (![cellObject.content isEqualToString:@""])
{
    UILabel *tempMessageLabel = [[UILabel alloc] init];

    tempMessageLabel.font = [UIFont systemFontOfSize:14];
    tempMessageLabel.autoresizingMask =  (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin );
    tempMessageLabel.numberOfLines = 5;
    tempMessageLabel.text = cellObject.content;
    [tempMessageLabel sizeToFit];
    CGFloat lineHeight = [tempMessageLabel.text sizeWithFont:tempMessageLabel.font].height;
    CGSize size = [tempMessageLabel.text sizeWithFont:tempMessageLabel.font
                                    constrainedToSize:CGSizeMake(280, lineHeight * tempMessageLabel.numberOfLines)
                                        lineBreakMode:UILineBreakModeWordWrap];
    messageHeight = size.height;

    if(self.contentExpanded)
        messageHeight += 100;
}
return messageHeight;
}

我想这已经被问到了:谢谢迈克!我已经查了好一阵子问题了,但还没有找到那个问题。我会检查一下,看看它是否解决了我的问题。稍后我会告诉你的。嗨,我已经编辑了我的问题。我没能让它工作。也许我做错了什么。请帮助,如果需要,我可以提供更多代码。您是否尝试删除BeginUpdate/EndUpdate?向我们显示您的
表格视图:HeightForRow索引路径:
  - (CGFloat)getHeightRowForCellObject:(CellObject *)cellObject
  {

CGFloat messageHeight = 0;

if (![cellObject.content isEqualToString:@""])
{
    UILabel *tempMessageLabel = [[UILabel alloc] init];

    tempMessageLabel.font = [UIFont systemFontOfSize:14];
    tempMessageLabel.autoresizingMask =  (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin );
    tempMessageLabel.numberOfLines = 5;
    tempMessageLabel.text = cellObject.content;
    [tempMessageLabel sizeToFit];
    CGFloat lineHeight = [tempMessageLabel.text sizeWithFont:tempMessageLabel.font].height;
    CGSize size = [tempMessageLabel.text sizeWithFont:tempMessageLabel.font
                                    constrainedToSize:CGSizeMake(280, lineHeight * tempMessageLabel.numberOfLines)
                                        lineBreakMode:UILineBreakModeWordWrap];
    messageHeight = size.height;

    if(self.contentExpanded)
        messageHeight += 100;
}
return messageHeight;
}