I have a realy big problem i cant solve. How can I link the menu name "Canada" in my expandable table menu to a new View Controller named "Canada", and then the menu name "Denmark" to a View Controller named "Denmark" and so on in my Xcode-project? Maybe by using segues?
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, ExpandableHeaderViewDelegate {
@IBOutlet weak var tableView: UITableView!
var sections = [
Section(genre: "North America",
menytitel: ["USA", "Canada"],
expanded: false),
Section(genre: "Europe",
menytitel: ["Sweden", "Finland", "Denmark", "Ireland"],
expanded: false),
Section(genre: "Africa",
menytitel: ["Egypt", "Marocko"],
expanded: false),
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].menytitel.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (sections[indexPath.section].expanded) {
return 44
} else {
return 0
}
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 2
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = ExpandableHeaderView()
header.customInit(title: sections[section].genre, section: section, delegate: self)
return header
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell")!
cell.textLabel?.text = sections[indexPath.section].menytitel[indexPath.row]
return cell
}
func toggleSection(header: ExpandableHeaderView, section: Int) {
sections[section].expanded = !sections[section].expanded
tableView.beginUpdates()
for i in 0 ..< sections[section].menytitel.count {
tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
}
tableView.endUpdates()
}
}
This is my section.swift file
import Foundation
struct Section {
var genre: String!
var menytitel: [String]!
var expanded: Bool!
init(genre: String, menytitel: [String], expanded: Bool) {
self.genre = genre
self.menytitel = menytitel
self.expanded = expanded
}
}