ios - uitableview 单元格在 ios 7 中自动调整大小。不止一个标签 [IMG]

标签 ios uitableview height autoresize

我有一个显示状态提要的表格 View (如下图所示),我试图弄清楚如何适本地调整每个单元格的大小,也就是具有较小单元格和显示较长状态的所有状态文本的较大单元格的短状态。我遇到过这个图:http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

但是,我仍然对如何计算按钮的额外空间或拇指 img 的左侧边距感到困惑。

目前它由 2 个按钮、一个标签(用于用户名)、一个缩略图和一个文本字段组成(不确定我是否应该使用文本字段或标签来显示状态,如果我这样做的话,或者对我来说没问题作品)

如果这个问题已经在某处得到回答,请提前打扰一下。我主要是一名刚开始使用 IOS 的 Android 开发人员,并且发现了很多关于这个主题的混合答案,所以我有点不确定 IOS7 中是否有解决这个问题的简单方法

enter image description here

最佳答案

有几种方法可以做到这一点,但我发现最可靠的方法是使用一个临时的、不可见的 UITextView 来获得必要的大小。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Ideally you should do lazy loading so that instead of creating a new textView each time, you just reuse the same one.
    UITextView *temp = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44)]; //This initial size doesn't matter
    temp.font = __Your_desired_font__
    temp.text = @"Put your status here";

    CGFloat textViewWidth = __your_constraint_width__
    CGRect tempFrame = CGRectMake(0,0,textViewWidth,44); //The height of this frame doesn't matter.
    CGSize tvsize = [temp sizeThatFits:CGSizeMake(tempFrame.size.width, tempFrame.size.height)]; //This calculates the necessary size so that all the text fits in the necessary width.

    //Add the height of the other UI elements inside your cell

    return tvsize.height + staticElementHeights


}

这适用于 iOS 7,但我认为如果你想支持 iOS 6,你需要实际添加 textView 作为 subview 才能工作。只需设置 temp.hidden = YES

文本在 UILabel 中的显示方式与 UITextView 不同,因此对于 UILabel,您应该使用此答案中描述的方法 iOS auto adjust label height

关于ios - uitableview 单元格在 ios 7 中自动调整大小。不止一个标签 [IMG],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20622629/

相关文章:

Ios 不支持的颜色空间错误

ios - Apple在这里使用什么控件? (包括截图)UIButton? UITableViewCell?还有别的吗?

ios - UITableView Cells - 如何使一个单元格的高度固定,其他单元格的高度相对于屏幕尺寸可变?

ios - 多设备混合应用程序 : Use Xcode6 and iOS8 SDK

ios - Swift ResearchKit makePDFWithCompletionHandler

ios - 如何根据内容改变桌面 View 高度

ios - 如何返回两个唯一值之一并避免 "Control may reach end of non-void function"

ios - UITableViewController 不会调用 cellForRowAtIndexPath 但 numberOfSectionsInTableView 和 numberOfRowsInSection 确实设置正确

asp.net - 如何从 C# 代码隐藏设置 iframe 的最小高度?

jquery - fancybox 宽度不适用