Ahmad Taalab
Ahmad Taalab

Reputation: 215

Show / Hide Sections in table view based on selection

i developing ipad application, this application for assets requests, i have 6 checkboxes and table view like the imageenter image description here

i created 6 sections in table, i need when user select for ex laptop in table view only laptop section will appear and other sections will removed or hide. this is my code

@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

    }



}

can you help me

Upvotes: 0

Views: 94

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You can try

 struct Item {

   var name:String

   var isShown:Bool
}

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

in numberOfSections

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

in titleForHeader

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

Upvotes: 1

Related Questions