Ghiggz Pikkoro
Ghiggz Pikkoro

Reputation: 146

How to hide or show a tableView when a button is tapped

I'm trying to show then hide a button when user tapped on a button. But it can't work at all, but it seems my code is correct in my mind.

Here's my code:

class ViewController: UIViewController {

    @IBOutlet weak var oeilButton: UIButton!
    @IBOutlet weak var tableView: UITableView!

    var clicked: Bool!

    override func viewDidLoad() {
        super.viewDidLoad()
        clicked = false
        tableView.isHidden = true // First it is hidden
    }

    @IBAction func oeilAction(_ sender: Any) {
        hideTable()
    }

    func hideTable() {
        if clicked == false {
            clicked = true
            tableView.isHidden = false
        } else {
            clicked = false
            tableView.isHidden = true
        }
    }
}

How can I do this? Help please

Upvotes: 0

Views: 190

Answers (1)

Mo Abdul-Hameed
Mo Abdul-Hameed

Reputation: 6110

Replace hideTable implementation with:

func hideTable() {
    tableView.isHidden.toggle()
}

This way you won't need a click variable and you won't need to check for anything.

Upvotes: 3

Related Questions