如何在iOS7上支持自定义单元格?

Eri*_*eim 14 objective-c ios ios7 swift ios8

随着iOS8的发布,我设计了我的桌面视图,细胞利用了自我调整大小的细胞.但我也需要我的表在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)]>"
)
Run Code Online (Sandbox Code Playgroud)

Eri*_*eim 25

这是我到目前为止找到的解决方案,但它需要检查特定的版本号而不是功能.您只需设置UITableViewAutomaticDimensioniOS 8或更高版本:

override func viewDidLoad() {

    if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 {
        self.tableView.estimatedRowHeight = 80
        self.tableView.rowHeight = UITableViewAutomaticDimension
    }
}
Run Code Online (Sandbox Code Playgroud)

对于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
}
Run Code Online (Sandbox Code Playgroud)