I have a table with some items and sections for those items. Both the items and sections are hard-coded and I'm having a little trouble understanding how I could rather have everything loaded from one array rather than two.
Here is my code:
import UIKit
class Beta: UIViewController, UITableViewDataSource, UITableViewDelegate{
@IBOutlet weak var tableView: UITableView!
let section = ["Fruits", "Vegetables"]
let items = [["Apple", "Banana"], ["Carrots", "Broccoli"]]
override func viewDidLoad(){
super.viewDidLoad()
self.tableView.allowsMultipleSelection = true
}
override func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.items[section].count
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.section.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
return self.section[section]
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.items[indexPath.section][indexPath.row]
//MARK: -Checkmark and save support.
cell.accessoryType = cell.isSelected ? .checkmark : .none
cell.selectionStyle = .none // to prevent cells from being "highlighted"
return cell
}
}
}
I used code that I could find online and the only working code I could find had the sections and items seperate in two arrays.
How can I make it read everything off of this one array?
var Food:NSDictionary = [
//:Section Title : Items
"Fruits" : ["Apple","Banana"],
"Vegetables" : ["Carrots","Broccoli"]
]