如何使tableViewCell Size自动化?

时间:2017-07-13 11:23:56

标签: ios swift uitableview

我有一个表viewCell并且有4个按钮但在某些情况下我隐藏了一个或多个按钮以及如何使表视图单元格的大小自动??? enter image description here

    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
}

2 个答案:

答案 0 :(得分: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

谢谢

答案 1 :(得分:0)

使用这样的按钮会增加复杂性。管理的可能性太多了。

选项1: 使按钮成为不同的单元格。每个问题都是TableView中的一个部分

选项2: 使用UITableView而不是按钮。 TableView的高度将是动态的,您不必管理不同的条件。