iphone - 基于内容 ios 的动态单元格高度

标签 iphone objective-c ios uitableview

我正在开发一款加载 Facebook 用户帖子的应用程序。所有这些信息都放在一个 TableView 中。当你点击它时,细胞是消耗性的。现在里面只有文字。我计算文本的高度,这样我就知道展开时单元格需要多高。

我遇到的问题是其中需要的不仅仅是文本。如果用户在 facebook 上发布视频,该视频也将嵌入到单元格中。但是由于高度是根据文本计算的,因此没有为嵌入的视频留出空间。我可以在单元格中添加边距,但此边距将应用于所有单元格。

这是我现在使用的代码:

//Function executes everything within when a certain row in tapped/selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    [self.tableView reloadData];
    [self stopLoading];
    if (selectedIndexPath == indexPath) 
    {         
        selectedIndexPath = nil;
    }
    else 
    {        
        selectedIndexPath = indexPath;
    }          
    [self.tableView deselectRowAtIndexPath : indexPath animated : YES];
    [tableView beginUpdates];
    [tableView endUpdates];
}

//Change heigth of the selected row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if ((selectedIndexPath != nil) && (selectedIndexPath.row == indexPath.row)) 
    {
        labelSize = [[result2 objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:13.0] 
                                                                  constrainedToSize:CGSizeMake(220.0f, MAXFLOAT) 
                                                                lineBreakMode:UILineBreakModeWordWrap];
        return labelSize.height + 136;

    }
    return 68.0;
}  

所以我需要知道什么。如何根据所有内容计算单元格的高度?而不仅仅是文本。

如果有人可以提供帮助,那就太好了!

提前致谢

最佳答案

其实……很简单。只需在 heightForRowAtIndexPath 中添加以下内容

    NSString *video = [fbSource objectAtIndex:indexPath.row] ; 
    if (video != @"empty") { return labelSize.height + 136; } 
    return labelSize.height + 36;

关于iphone - 基于内容 ios 的动态单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5748309/

相关文章:

iphone - 在 Textview 中共享数据并维护数据状态不起作用

ios - UICollectionView allowsMultipleSelections 不工作

ios - 当单元格出现时使用 AutoLayout 为 UITableViewCell 内容设置动画

iOS Alamofire 不正确的授权 header

iphone - 如何将 bool 值存储到 NSArray?

iphone - 我们可以使用 SLComposeViewController 在 FB 上共享视频吗?

ios - UITableViewCell 选中行的文字颜色变化

ios - Xcrun 没有嵌入传递的配置文件

iphone - 如何强制coredata重建sqlite数据库模型?

iphone - 从 Objective-C 类调用 C++ 函数