2015-12-22 21 views
2

我有一個自定義UITableViewCell佈局,看起來像這樣。它有三個標籤。動態地將視圖移動到自動佈局中的新位置

enter image description here

標籤2是一個可選的一個。它不存在於每個細胞中。所以我想隱藏和移動標籤1調低一點是中心與標籤3當這種情況發生對齊。

enter image description here

下面是我添加每個標籤的限制。

標籤1

enter image description here

標籤2

enter image description here

標籤3

enter image description here

通知我已經加入一個額外的約束,對齊中心至Y爲0的值,以標籤1並其優先級設置爲750。我想如果除去標籤2,該約束用低優先級將取代它的位置並向下移動。

class TableViewController: UITableViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    // MARK: - Table view data source 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 3 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell 

     if indexPath.row == 1 { 
      cell.label2.removeFromSuperview() 
      cell.updateConstraints() 
     } 

     return cell 
    } 
} 

但它似乎沒有工作。 標籤2被刪除,但標籤1的位置仍然相同。

enter image description here

我怎樣才能做到我所追求的?


嘗試#1

按低於T先生的answer,我添加了一個頂部約束到標籤1。然後在cellForRowAtIndexPath方法中,我改變了它的值。

override func tableView(tableView: UITableView, cellForRowAtIndexPath 

    indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell 

     if indexPath.row == 1 { 
      cell.label2.removeFromSuperview() 
      cell.topConstraint.constant = cell.bounds.height/2 
      cell.layoutIfNeeded() 
     } 

     return cell 
    } 

但是這也沒有工作。

+0

對'layoutIfNeeded'一試? – zcui93

+0

@ zcui93剛試過。不用找了。 – Isuru

+0

我注意到標籤1的'CentreY'約束都是針對'superview'的,如果第二個涉及標籤2? – zcui93

回答

0

我想出了一個辦法做到這一點利用如answer中所述的NSLayoutConstraint的新active財產。

我做了一個IBOutlet對齊中心Y約束值-13。從中刪除了weak關鍵字。

然後在cellForRowAtIndexPath方法中,我只是簡單地切換active屬性的值。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell 

    if indexPath.row % 2 == 0 { 
     cell.label2.hidden = true 
     cell.oldConstraint.active = false 
    } else { 
     cell.label2.hidden = false 
     cell.oldConstraint.active = true 
    } 

    return cell 
} 

enter image description here

+0

我仍然不認爲這是一個最好的解決方案。由於標籤具有固有尺寸,因此只能通過將標籤文本設置爲零來達到此目的。將檢查明天並帶回答 –

0

嘗試有針對標籤1.頂部約束的出口,並且當您刪除標籤2,更新這是container.height/2或刪除頂部約束,並給予centerY的label1的頂部約束約束到標籤1.如果需要更新約束,則執行佈局。

+0

剛剛試了一下。但不幸的是,它似乎並不奏效。 – Isuru

+0

你可以添加你試過的問題嗎? –

+0

更新了問題。 – Isuru

0

我非常深刻的印象多少你來,只要那會是正好,如果我不得不完成它:)

現在我的建議,我會遵循相同的步驟,方式: 刪除一個額外的「對齊中心到Y的值0到標籤1「,你已經添加了,沒有任何用途:)

我可以看到你已經有一個對齊中心y與一些偏移我相信-13標籤1。創建一個iboutlet的:)說我們說它的名字爲centerLabel1Constraint :)

只要你想brin摹標籤1至中心隱藏標籤2,並設置centerLabel1Constraint.constant = 0,並調用[細胞layoutIfNeeded]

這應該做的工作:) 編碼愉快:)

+0

感謝您的回覆。我試過這個。它的工作,但只是部分。問題是沒有辦法確定具有標籤2的單元格的常量值,因爲一旦將其設置爲0,它將在單元格重用時重用。 – Isuru

+0

你已經知道常數值了嗎?我的意思是,當你想顯示label2時,只需將其顯示爲可見,並將centerLabel1Constraint.constant的值設置回-13並調用[cell layoutifneeded] –

+0

總結:您在indexpath處的行的單元格看起來像if(condition:show label2){ centerLabel1Constraint.constant = -13; label2.Hidden = NO; [cell layoutIfNeeded]} else {{centerLabel1Constraint.constant = 0; label2.Hidden = YES; [cell layoutIfNeeded]}是不是? :) –