How do I support self sizing cells on iOS7?

前端 未结 2 506
眼角桃花
眼角桃花 2020-12-29 09:23

With the release of iOS8 I have designed my table view with cells taking advantage of self sizing cells. But I need my tables to work in iOS7 as well. How do I do that? Is t

相关标签:
2条回答
  • 2020-12-29 09:30

    This is the solution I have found thus far, but it requires checking for specific version number rather than capability. You only set UITableViewAutomaticDimension if you have iOS 8 or higher as version:

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

    For iOS 7 you need to calculate a height for each cell. But if you are on iOS 8 you can return UITableViewAutomaticDimension as the height:

    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
    }
    
    0 讨论(0)
  • 2020-12-29 09:35

    For iOS versions older than 8.0, you can always write the usual heightForRowAtIndexPath method where you create a cell, run an auto layout pass on it and then return the actual height.

    0 讨论(0)
提交回复
热议问题