2017-07-13 17 views
0

我有一個表viewCell,並有4個按鈕,但在某些情況下,我隱藏了一個或多個按鈕,以及如何使表視圖單元格的大小自動? enter image description here如何使tableViewCell Size自動?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionCell")! 
    variant1 = cell.contentView.viewWithTag(1) as! UIButton 
    variant2 = cell.contentView.viewWithTag(2) as! UIButton 
    variant3 = cell.contentView.viewWithTag(3) as! UIButton 
    variant4 = cell.contentView.viewWithTag(4) as! UIButton 

    let questionTextView = cell.contentView.viewWithTag(5) as! UITextView 
    questionTextView.text = "\(Questions[indexPath.row].content!), подсказка: \(Questions[indexPath.row].hint!)" 
    variant1.addTarget(self, action: #selector(self.variant1ButtonPressed), for: .touchUpInside) 
    variant2.addTarget(self, action: #selector(self.variant2ButtonPressed), for: .touchUpInside) 
    variant3.addTarget(self, action: #selector(self.variant3ButtonPressed), for: .touchUpInside) 
    variant4.addTarget(self, action: #selector(self.variant4ButtonPressed), for: .touchUpInside) 
    //cell.contentView.backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 0.65) 

    let numberOfVars = numberOfVariants[indexPath.row] 
    if (numberOfVars == 2) { 
     variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal) 
     variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal) 
     variant3.isHidden = true 
     variant4.isHidden = true 
    } 
    else if (numberOfVars == 3){ 
     variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal) 
     variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal) 
     variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal) 
     variant4.isHidden = true 
    } 
    else if (numberOfVars == 4) { 
     variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal) 
     variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal) 
     variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal) 
     variant4.setTitle(Variants[3+amaunty[indexPath.row]].title, for: .normal) 
    } 
    return cell 
} 
+0

是這個單元被設計成在故事板或動態細胞靜細胞? –

+0

在按鈕上添加高度約束併爲約束創建出口。如果要隱藏某個按鈕,只需使用其相應的高度約束出口將其高度設置爲0即可。如果需要更多幫助,請同時使用此按鈕。https://www.raywenderlich。 com/129059/self-sizing-table-view-cells – sanman

+0

在單元格中使用StackView並使表格視圖單元高度動態化。當您隱藏按鈕時,堆疊視圖將自動管理高度。 – Ujesh

回答

2

在UITableViewCell中使用UIStackView。 在stackview中添加你的按鈕,並給所有四個約束stackview不給定高度寬度。 現在在行索引的單元格中根據您的條件隱藏任何按鈕。

此代碼適用於UITableViewCell的動態高度。

extension ViewController : UITableViewDelegate,UITableViewDataSource { 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ButtonCell 

    switch indexPath.row { 
    case 1: 
     cell.btn1.isHidden = true 
     break 
    case 2: 
     cell.btn2.isHidden = true 
     break 
    case 3: 
     cell.btn3.isHidden = true 
     cell.btn2.isHidden = true 
     break 
    case 4: 
     cell.btn4.isHidden = true 
     cell.btn3.isHidden = true 
     cell.btn2.isHidden = true 
     break 
    default: 
     print("Default") 
    } 

    return cell 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 5 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 
} 

第一張截圖顯示了stackview控件的屬性。

enter image description here

第二個屏幕顯示您給予stackview的所有四個約束,並給予高度約束到所有按鈕。

enter image description here

最終響應

enter image description here

謝謝

0

這樣使用按鈕會增加複雜性。有太多的可能性來管理。

選項1: 使按鈕成爲不同的單元格。每個問題將在TableView中的一個部分

選項2: 使用UITableView而不是按鈕。 TableView的高度將是動態的,您不必管理不同的條件。