我如何在iOS7上支持自定义大小的单元格?

随着iOS8的发布,我devise了我的表格视图,利用自定义单元格的单元格。 但是我需要我的桌子在iOS7中工作。 我怎么做? 有没有办法在运行时检查自定义单元格是否被支持,或者我可以在我的控制器中实现一些不会在iOS7中调用的表格委托方法?

如果我在iOS7中尝试使用自定义单元格的表格,则会在控制台上看到以下错误:

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSLayoutConstraint:0x7fc912d1d5a0 V:|-(>=11)-[UILabel:0x7fc912d13900] (Names: '|':UITableViewCellContentView:0x7fc912d13400 )>", "<NSLayoutConstraint:0x7fc912d1d6b0 V:[UILabel:0x7fc912d13900]-(11)-| (Names: '|':UITableViewCellContentView:0x7fc912d13400 )>", "<NSAutoresizingMaskLayoutConstraint:0x7fc912d24d80 h=--& v=--& V:[UITableViewCellContentView:0x7fc912d13400(0.5)]>" ) 

这是迄今为止我find的解决scheme,但它需要检查具体版本号而不是能力。 如果您有iOS 8或更高版本,则只设置UITableViewAutomaticDimension

 override func viewDidLoad() { if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 { self.tableView.estimatedRowHeight = 80 self.tableView.rowHeight = UITableViewAutomaticDimension } } 

对于iOS 7,您需要计算每个单元格的高度。 但是如果你在iOS 8上,你可以返回UITableViewAutomaticDimension作为高度:

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 { return UITableViewAutomaticDimension } return 50 // Or whatever calculated value you need for cell height } 

对于8.0以前的iOS版本,您可以始终在创build单元格的位置编写通常的heightForRowAtIndexPath方法,对其执行自动布局传递,然后返回实际高度。