2017-05-30 54 views
0

我有兩個單元(一個動態等靜態),我要像每個小區設置不同的高度:在我的UITableView特定細胞集高度

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     /*tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath) 
     tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) 
     if cell1 return 100 
     if cell2 return 20 */ 
    } 

這是可能的具體高度爲每個小區不行。
我怎樣才能解決這個問題?

回答

2

對於靜態單元(創建無論是作爲廈門國際銀行或在故事板),你可以設置這樣的高度,如果你是顯示靜態細胞在你的Table View的第一行。

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
if indexPath.row == 0 { 
    return 120 
} 

return UITableViewAutomaticDimension 

}

要使用動態小區沿填充靜態的細胞,你應該做的,

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return datasource.count + 1 

}

1

UITableViewAutomaticDimension

https://www.raywenderlich.com/129059/self-sizing-table-view-cells -

如果你正確設定,您可以使用UITableViewAutomaticDimension有大小,而不需要指定單元格自動佈局。

+0

如果您仍然要重新定義手動您必須在細胞地圖由indexPath.row和indexPath.section。您必須知道第2行的實例,因爲此時單元格尚未初始化。 – teixeiras

+0

你得到了點,我不知道爲什麼每個人都在這裏得到的,而不是使用一些通用的方法 –

0
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     if indexPath.row == 0 { 
      return 100 
     } else { 
      return 20 
     } 
} 
+0

感謝@MichałKwiecień特定行,我已經試過了,它的工作,但第一個單元格不會出現 – Akram

1

*在,雨燕3.0

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

    if indexPath.row == 0 { 

     return 100 
    } else { 

     return 200 
    } 

} 

,或者你可以設置你的細胞適當的約束,然後寫你的ViewDidLoad方法的驗證碼

yourTblView.estimatedRowHeight = 100 

yourTblView.rowHeight = UITableViewAutomaticDimension* 
+0

感謝@Jaydip但我不工作,因爲我有2單元一個動態等靜態我想知道是否有可能對特定細胞不能行 – Akram

+0

兒子的情況下,你可以試試字典的數組,並設置關鍵單元類型和設置。 – Jaydip

0

你需要爲每個indexPath返回一個高度。這裏不需要出隊。如果你想要不同的原型單元,你會在cellForRowAtIndexPath中做到這一點。

,你想,你可以指定許多部分和行:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     switch indexPath.section { 
     case 0: 
      switch indexPath.row { 
      case 0: 
       return 100.0 
      case 1: 
       return 20.0 
      // Add rows here if needed 
      default: 
       return UITableViewAutomaticDimension 
     // Add sections here if needed 
     default: 
      default: 
       return UITableViewAutomaticDimension 
     } 
} 
+0

OK @pesch和感謝,但如何使用的cellForRowAtIndexPath? – Akram

+0

你不用它。它將與從委託每次調用'時間的其餘功能被稱爲tableView.reloadData()'例如 – pesch

+0

仍然沒有工作對我來說@pesch – Akram