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()
}
CodePudding user response:
Ugh, try creating an if condition for each segue and .isHidden property. That works 100% fam
CodePudding user response:
To achieve what you want you can try following:
Create an enum:
enum ShowCase: Int{
case items, menu
var headerTitle: String{
switch self {
case .items:
return "Section 1"
case .menu:
return "Section 2"
}
}
static func createFromSegueIdentifier(_ str: String) -> ShowCase{
str == "segueItemsOption" ? .items : .menu
}
}
In you ViewController add:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let vc = segue.destination as? ViewControllerTwo, let identifier = segue.identifier else{
return
}
vc.showCase = ShowCase.createFromSegueIdentifier(identifier)
}
in ViewcontrollerTwo add an implicitly unwrapped var:
var showCase: ShowCase!
and the extension should look like:
extension ViewControllerTwo: UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
// return only one section
1
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
showCase.headerTitle
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
"Number of items in \(showCase.headerTitle.lowercased()) is \(dataArray[showCase.rawValue].count)"
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray[showCase.rawValue].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[showCase.rawValue][indexPath.row]
return cell!
}
}