2

我有一个自定义单元格,其中一个标签可以大到足以证明添加“查看更多”按钮是合理的。因此,我将标签的内容截断为 5 行,然后添加此按钮以扩展内容(如果需要)。

我的问题是:这可以在不使用 tableView 的 reloadData 的情况下完成吗?

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

  self.contentView.frame = self.backgroundView.frame = self.accessoryView.frame =  CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height + 80);

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

提前致谢,


版:我在链接ios - Expand table view cell like Twitter app中检查了解决方案,我已在自定义单元格中应用了此代码(在触摸事件上):

self.contentExpanded = YES;
UITableView *tableView = [self parentTableView];
NSIndexPath *indexPath = [tableView indexPathForCell:self];


[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

我现在有 2 个问题:1) reloadRowsAtIndexPaths 正在执行整个 reloadData。2)自定义单元格是知道单元格高度的那个。访问此方法时,self.contentExpanded 始终等于 NO。

***方法 parentTableView 取自对单元格的父 tableView 的引用


第 2 版:我认为问题不在于 heightForRow。我无法避免为所有单元格调用此方法...

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

}

在自定义单元格中:

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

0 回答 0