I have a storyboard with two buttons, each one with target to another storyboard with a TableView but different segue identifier.
Right now I populate my data source through a two dimension array
var dataArray = [["a", "b", "c"], ["menu 1", "menu 2", "menu 3"]]
What I would like to do is based on the button selection hide a TableView section.
Example:
Selected button 1 hide section 2 on my TableView.
&
Selected button 2 hide section 1 on my TableView.
My data source extension looks like this
extension ViewControllerTwo: UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return dataArray.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0{
return "Section 1"
}
return "Section 2"
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
if section == 0{
return "Number of items in section 1 is (dataArray[0].count)"
}
return "Number of items in section 2 is (dataArray[1].count)"
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "singleCell")
if cell == nil{
cell = UITableViewCell(style: .default, reuseIdentifier: "singleCell")
cell?.accessoryType = .detailButton
}
cell!.textLabel?.text = dataArray[indexPath.section][indexPath.row]
return cell!
}
}
The two viewControllers are named as follow
- "ViewController"
- "ViewControllerTwo"
Think of the segues identifiers named as follow
- "segueItemsOption" for button 1
- "segueMenuOption" for button 2
Edit
View Controller Code.
Haven’t really modified anything as segue automatically displays the next VC.
import UIKit
class ViewController: UIViewController {
// MARK: Outlets
@IBOutlet weak var itemsButton: UIButton!
@IBOutlet weak var menuButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Buttons Rounded Corner
itemsButton.round()
itemsButton.round()
}
// MARK: Button Actions
@IBAction func itesmButtonAction(_ sender: Any) {
createVolumes.shine()
}
@IBAction func menuButtonAction(_ sender: Any) {
listVolumes.shine()
}
2
Answers
Ugh, try creating an if condition for each segue and .isHidden property.
That works 100% fam
To achieve what you want you can try following:
Create an enum:
In you ViewController add:
in ViewcontrollerTwo add an implicitly unwrapped var:
and the extension should look like: