2016-08-01 65 views
1

當創建一個單元格:如何以編程方式更改UITableViewCell上的約束?

func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

原來cell.constraints.count等於0,即使包含電池佈局的廈門國際銀行是擠滿了他們。實現以下兩個功能 - 這我想象會在適當時刻被稱作 - 產生同樣令人失望的結果:

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) 

,如果我不能訪問的約束,我怎能變化他們?

我在這裏做錯了什麼?謝謝。

+0

您是否嘗試過繼承'UITableViewCell'並在子類中訪問您的約束? – Wez

+0

您可以在UITableVIewCell中創建插座,但是如果您需要在UITableViewController中獲取約束,則可以通過cell.contentView.constarints –

回答

1

使IBOutlet爲你constraint在你的UITableViewCell類然後做cellforrowatindexpath方法的變化。

例如:在cellforrowatindexpath

cell.texlabel.topspace.constant = 100; 
+0

訪問它們。謝謝@AnuradhS,我非常喜歡您的解決方案。 –

+0

不客氣@TheMotherofJosephBeuys –

+0

如果我改變頂部空間的細胞高度怎麼樣?它是動態變化的嗎? –

1

嘗試使用

cell.contentView.constraints 

這對我的作品!

相關問題