首页 > 解决方案 > 使一个部分在其他部分的 Button 操作上可见

问题描述

我在 tableview 上显示多个部分的数据,

图片

当我打开第 1 部分的 tableView 高度应该为 0。如果我单击第 0 部分中的按钮,第 1 部分的高度将为 150。例如,如果我单击(想要)部分单元格显示 150 的代理按钮高度,我单击生成器按钮部分高度将为 0

我想让第 1 部分在从第 0 部分单击的按钮上可见

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.section == 0
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! postaddTableViewCell1

            cell.ownerButton.addTarget(self, action: #selector(FisrtButtonClick), for: .touchUpInside)
            cell.agentButton.addTarget(self, action: #selector(FisrtButtonClick), for: .touchUpInside)
            cell.builderButton.addTarget(self, action: #selector(FisrtButtonClick), for: .touchUpInside)
            return cell

        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! postaddTableViewCell2
            cell.checkbutton.tag = indexPath.row
            cell.checkbutton.addTarget(self, action: #selector(checkbuttonClick) , for: .touchUpInside)
            return cell
        }
    }

  @objc func FisrtButtonClick(_ sender :UIButton)
    {
        let indexPath = IndexPath(row: 0, section: 0)
        let cell = TableView.cellForRow(at: indexPath) as? postaddTableViewCell1

        print(cell?.agentButton.titleLabel as Any)

        if sender.tag == 10
        {
            cell?.ownerButton.backgroundColor = #colorLiteral(red: 0, green: 0.7254901961, blue: 0.4588235294, alpha: 1)
            cell?.agentButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
            cell?.builderButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
        }else if sender.tag == 11
        {
            cell?.ownerButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
            cell?.agentButton.backgroundColor = #colorLiteral(red: 0, green: 0.7254901961, blue: 0.4588235294, alpha: 1)
            cell?.builderButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)


        } else if sender.tag == 12
        {
            cell?.ownerButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
            cell?.agentButton.backgroundColor = #colorLiteral(red: 0.768627451, green: 0.768627451, blue: 0.768627451, alpha: 1)
            cell?.builderButton.backgroundColor = #colorLiteral(red: 0, green: 0.7254901961, blue: 0.4588235294, alpha: 1)
        }

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

        if indexPath.section == 0
        {
            return 80
        }
        if indexPath.section == 1
        {
            return 0
        }
        return 80
     }

标签: iosswiftuitableview

解决方案


检查是否在方法中单击了所需的按钮FisrtButtonClick并设置了boolean可访问的heightForRowAt. 切换布尔值后,您可以重新加载您需要的部分(也清理了您的代码 lil 位)。

var selectedAgent = false

@objc func firstButtonClick(_ sender: UIButton) {
    if sender.currentTitle == "Agent" {
        selectedAgent = true
        tableView.reloadSections([1], with: .automatic)
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == 1 && !selectedAgent {
        return 0
    }
    return 80
}

推荐阅读