2016-09-16 286 views
2

我使用UITableViewAutomaticDimension設置我的UITableView單元的高度。對於過濾功能,我需要隱藏一些單元格,我將通過將它們的高度設置爲0來隱藏它們。如何使用UITableViewAutomaticDimension手動設置某些單元格的高度設置?

但是,當我重寫heightForRowAtIndexPath時,我需要爲行自動返回一些值。我如何設置它?

tableView.rowHeight = UITableViewAutomaticDimension 
tableView.estimatedRowHeight = 50 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if shouldHideCell == true{ 
     return 0 
    }else{ 
     return automaticHeight // I need a value here!!! 
    } 
} 
+0

輝煌的問題。明智的答案。非常好,不是唯一的!感謝:-) :-)如果只有我可以,還有一百萬張選票。 – Benjohn

回答

3

在你的情況下嘗試設置UITableViewAutomaticDimension的方法,而不是在你的viewDidload,如:

override func viewDidLoad() { 
    super.viewDidLoad() 
    tableView.estimatedRowHeight = 50 
    .... 
} 


func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if shouldHideCell == true{ 
     return 0.0 
    }else{ 
     return UITableViewAutomaticDimension 
    } 
} 

我希望您使用autoLayoutsUITableViewAutomaticDimension工作。 而且請注意,在cellForRow方法之前調用heightForRowAtIndex,因此請確保您的變量shouldHideCell在此之前已正確設置。

+0

似乎很好,謝謝。 –

+0

ROCK! - 非常感謝。 – Benjohn

2

如果使用UITableViewAutomaticDimension我假設你正在使用自動佈局,對不對? 如果您使用自動佈局,您可以通過更改具有高度或固有高度的約束條件的常量來「隱藏」單元格。 試試這個。

+0

也是滾!謝謝。 – Benjohn