2014-10-09 37 views
14

隨着iOS8的發佈,我設計了我的表格視圖,利用細胞利用自調整細胞。但我需要我的桌子在iOS7中工作。我怎麼做?有沒有辦法在運行時檢查是否支持自定義大小的單元格,還是可以在控制器中實現一些不會在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)]>" 
) 
+3

你不能,iOS 7不支持iOS 8的自定義單元格。你將需要編寫一個'tableView:heightForRowAtIndexPath:'dat計算單元格的正確高度。 – rckoenes 2014-10-09 15:22:54

+0

如果您需要支持iOS7,請勿使用自行調整功能。 – CrimsonChris 2014-10-09 15:22:55

+1

我知道iOS7不支持自我尺寸測量單元。我的問題是如何處理爲iOS8設計的使用自調整大小的表格。例如。將tableView:heightForRowAtIndexPath:只能在iOS7上調用,而不是在iOS8上調用?總之,如何編寫適用於iOS7和iOS8的代碼? – 2014-10-09 15:34:33

回答

25

這是迄今爲止我已經找到了解決方案,但它需要檢查特定的版本號,而比能力。您只需設置UITableViewAutomaticDimension如果您有iOS版8或更高版本爲:

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 
} 
+1

爲我工作,但我需要添加()圍繞if語句。如果你返回if,你也不需要別的東西。 – 2014-11-12 21:03:50

+1

對於iOS版本比7.1更新的版本,這將會中斷。我寧願檢查功能(即使用'respondsToSelector')或<8.0。 – DrMickeyLauer 2015-05-08 13:28:17

+0

我該如何做到這一點對於客觀的C你可以張貼也是 – 2015-05-08 13:59:14

1

對於比8.0更舊的iOS版本,你可以寫,你創建一個細胞通常heightForRowAtIndexPath方法,運行自動佈局傳遞它然後返回實際高度。

相關問題