iOS8:静态单元格可以使用“tableview.rowHeight = UITableViewAutomaticDimension”吗?

我有一个UITableView有五个静态单元格。 我需要一个单元格的单元格高度自动调整其内容,这是一个UILabel。

有什么方法我可以使用..

tableView.estimatedRowHeight = 42.0 tableView.rowHeight = UITableViewAutomaticDimension 

..静态单元格的表格视图,还是这样做只适用于具有dynamic原型单元格的表格视图?

(或者还有其他的方法可以推荐吗?)

附加信息

UITableView是在一个TableViewController中embedded到一个容器视图。

这五个静态单元格彼此完全不同,只能在一个应用程序场景中使用,所以我没有看到dynamic原型和自定义单元格子类的许多要点。

我像这样更新tableview

 -(void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [_myTableView reloadData]; } 

我添加约束到UILabel这里描述: http : //www.appcoda.com/self-sizing-cells/

对于iOS 8,我发现你无法做到与dynamic表格视图单元相同的策略。

要自动调整静态单元格的高度,我实现了这两个UITableViewDelegate方法:

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44; } 

希望能帮助到你。

扩展胜利者的答案,基于静态单元的表格视图似乎自动化基于内容和约束的单元格,一旦下面的代表被实现:

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44; } 

除了有标签的时候。 由于某些原因,标签的内在高度似乎没有对细胞的高度计算有贡献。 我目前的工作是为了在UIView中嵌套标签。

请执行下列操作:

  1. 将标签(或标签)embedded到视图中。 (在IB上select标签,然后点击菜单>编辑器>embedded>查看)
  2. 在这个视图和单元之间build立水平和垂直的边界约束
  3. 在你的标签和这个视图之间build立横向和纵向的边缘限制。

当然,感觉像一个黑客,但在我尝试的所有情况下为我工作。

完成了这个。 不是我想要的,但它适用于我的简单要求和文本长度。

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 2) { CGFloat padding = 30.0f; CGFloat labelWidth = self.tableView.bounds.size.width - padding*2; NSAttributedString *text = [[NSAttributedString alloc] initWithString:_freeTextLabel.text]; NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin; CGRect boundingRect = [text boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX) options:options context:nil]; return (CGFloat) (ceil(boundingRect.size.height) + padding*2); } return [super tableView:tableView heightForRowAtIndexPath:indexPath]; } 

注意 :这是基于@CamelBase的答案,它是Objective-C编写的,所以大多数情况下只是对Swift的转换,对于那些开关,仍然很难转换的东西自己。


对于那些想要使用Swift来完成这个需要静态单元格的表格,这里有一个工作的解决scheme。

我不需要使用这个代码(尝试把它放在viewDidLoad()和尝试没有,没有什么区别):

 tableView.estimatedRowHeight = 42.0 tableView.rowHeight = UITableViewAutomaticDimension 

首先,您要将UILabel添加到您希望自动增长内容的每个单元格中。

你将需要添加4个约束到UILabel所有相对于父(内容视图的单元格)。

  • 领先的保证金0
  • 尾随保证金0
  • 上限0
  • 底部保证金0

这将使标签在单元格增长时增长(我们的代码如下)。

现在只需添加下面的代码:

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { let sectionICareAbout = 2 let rowICareAbout = 1 // repeat for as many section/row combos you need dynamic sizing on if indexPath.section == sectionICareAbout && indexPath == rowICareAbout { // replace with how you get the content for the cell // in section 2 row 1 let text = "line1\nline2\nline3" return rowHeightForText(text) } // use auto-dimension if we didn't set something else return UITableViewAutomaticDimension } func rowHeightForText(text: String) -> CGFloat { let labelWidth = tableView.bounds.size.width let attributedText = NSAttributedString(string: text) let options = NSStringDrawingOptions.UsesLineFragmentOrigin let size = CGSize(width: labelWidth, height: CGFloat.max) let boundingRect = attributedText.boundingRectWithSize(size, options: options, context: nil) return boundingRect.size.height } 

在IOS8中,我发现在代码中明确设置行高/宽度或标签高度/宽度会导致比修复更多的问题。 我已经使用自动布局创build了具有不同单元格高度和多行标签的dynamic和静态表格,但是您确实必须遵循苹果标准来处理字母或奇怪的事情(如分隔符消失或行随机高度塌缩)。

  • [编辑]将estimatedHeightForRowAtIndexPath heightForRowAtIndexPath设置为UITableViewDelegate中的自动尺寸

     override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } 
  • 确保单元格中的每个元素都有一个顶部/底部/左/右=约束(不是> =,<=或者alignX或者alignY–它必须是=)。 您可以将此作为一个低优先级的约束,并提供更高优先级的更好约束,但是您必须给它一个确切的初始值来确定单元的大小。

下面是Swift 3的一个简单例子。在Storyboard中,我们有一个UITableViewController ,其中包含types分组静态单元格的内容。 它的tableView包含一个UITableViewCell 。 单元格包含一个UILabel ,其行数设置为0,并显示一些非常长的文本。 该标签还具有四个自动布局约束与其superView顶部底部领先尾随 )。

在这里输入图像说明

现在,您可以select以下四个代码片段之一,以便您的静态单元根据其内容自动调整其大小。


#1。 使用tableView(_:estimatedHeightForRowAt:)tableView(_:heightForRowAt:)

注意: tableView(_:estimatedHeightForRowAt:)方法需要iOS7

 import UIKit class TableViewController: UITableViewController { override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return 100 // Return `UITableViewAutomaticDimension` if you have no estimate } } 

#2。 使用estimatedRowHeight属性和tableView(_:heightForRowAt:)

注意: estimatedRowHeight属性需要iOS7

 import UIKit class TableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() tableView.estimatedRowHeight = 100 } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } } 

#3。 使用tableView(_:heightForRowAt:)

 import UIKit class TableViewController: UITableViewController { // Bind this IBOutlet to the cell in Storyboard @IBOutlet weak var cell: UITableViewCell! override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { let fittingSize = CGSize(width: tableView.bounds.size.width, height: 0) let systemLayoutSize = cell.systemLayoutSizeFitting(fittingSize, withHorizontalFittingPriority: UILayoutPriorityRequired, verticalFittingPriority: UILayoutPriorityFittingSizeLevel) return systemLayoutSize.height } } 

#4。 使用UITableViewCell子类和tableView(_:heightForRowAt:)

 import UIKit class TableViewController: UITableViewController { // 1. Set custom class to `CustomCell` for cell in Storyboard // 2. Bind this IBOutlet to the cell in Storyboard @IBOutlet weak var cell: CustomCell! override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { cell.layoutIfNeeded() let contentViewSize = cell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize) return contentViewSize.height + 1 // + 1 for the separator } } class CustomCell: UITableViewCell { // 1. Set custom class to `CustomCell` for cell in Storyboard // 2. Bind this IBOutlet to the cell's label in Storyboard @IBOutlet weak var label: UILabel! override func layoutSubviews() { super.layoutSubviews() label.preferredMaxLayoutWidth = frame.width } } 

之前的所有四个代码片段将导致以下显示:

我假设我需要告诉单元格重绘使用类似的东西

 [self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone]; [self.tableView endUpdates]; 

当涉及到dynamic改变tableviewcells的高度。

 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return Yourvalue; //return whatever u want ur cell to have size. } 

我也在寻找这个答案,并find了这个解决scheme,我testing和完美的iOS8。

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 2) //Dynamic Height { return UITableViewAutomaticDimension; } else { return 50; // Static Height } }