swift - 根据选择在 TableView 中显示/隐藏部分

标签 swift

我正在开发 ipad 应用程序,此应用程序用于 Assets 请求, 我有 6 个复选框和表格 View ,如图所示 enter image description here

我在表中创建了 6 个部分,当用户在 TableView 中选择前笔记本电脑时,我需要只显示笔记本电脑部分,而其他部分将被删除或隐藏。 这是我的代码

@objc func circleBoxValueChanged(sender: Checkbox) {
    tblView.isHidden = false


    switch sender.tag {
    case 1:
        print("1")
    case 2:
        print("2")
    case 3:
        print("3")
    case 4:
        print("4")
    case 5:
        print("5")
    case 6:
        print("6")
    default:
        break
    }
    print("circle box value change: \(sender.isChecked)")
}


// MARK: - TableView Delegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 200
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 6
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
        return "Laptop"
    }else if section == 1 {
        return "Mobile"
    }else if section == 2 {
        return "Ex Badge"
    }
    else if section == 3 {
        return "VIP Badge"
    }
    else if section == 4 {
        return "Gift"
    }
    else if section == 5{
        return "Locker"
    }else{

        return "Other"

    }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return 1

    }else if section == 1{
        return 1

    }else if section == 2{
        return 1

    }else if section == 3{
        return 1

    }else if section == 4{
        return 1

    }else if section == 5{
        return 1

    }else{
        return 0

    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    switch indexPath.section {
    case 0:
        let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! laptop_MobileTableViewCell
        return cell1

    case 1:
        let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! laptop_MobileTableViewCell
        return cell1

    case 2:
        let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! OtherRequstTableViewCell
        return cell1

    case 3:
        let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! OtherRequstTableViewCell
        return cell1
    case 4:
        let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! OtherRequstTableViewCell
        return cell1

    case 5:
        let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! OtherRequstTableViewCell
        return cell1


    default:
        let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! OtherRequstTableViewCell
        return cell1

    }



}

你能帮帮我吗

最佳答案

你可以试试

 struct Item {

   var name:String

   var isShown:Bool
}

let arr = [Item(name:"LapTop",isShown:true)] // and so--on

numberOfSections

return arr.filter {$0.isShown}.count

titleForHeader

return arr.filter{$0.isShown}[indexPath.section].name

关于swift - 根据选择在 TableView 中显示/隐藏部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51588091/

相关文章:

ios - 选择器 View 快速出错

swift - 这是什么原因造成的? - "CoreAnimation: warning, deleted thread with uncommitted CATransaction"

ios - 无法将UITextField聚焦在嵌套的UIView中

ios - 如何使用 segues 在 View Controller 之间传输数据

ios - Xcode 12.5 Playgrounds 不显示任何输出或运行代码

swift - 代码的文本域识别

ios - 改 rebase 于 uilabel 的自适应布局的颜色

swift - 如何使我的导航栏快速透明?

ios - 使用 ContactsUI 快速获取所有联系电话号码

ios - Firebase 观察者架构