Separate sections for each Headings; Under each section title should be SubHeadings, subtitle should be Drugs.
I am able to get numberOfSections
correctly, how can I set the right section Header and call title and subtitle for this project?
class FinalPageViewController: UITableViewController {
var top: [String: AnyObject]!
var subheading: [String: AnyObject]!
var drugs:[String: AnyObject]!
let CellIdentifier = "Cell Identifier"
var sectioHeader = [String] ()
var subheads: [AnyObject] {
//Passing the key Top(Array) here and defining it from plist
if let subheads = top["Subhead"] as? [AnyObject] {
return subheads
} else {
return [AnyObject]()
}
}
var drug: [AnyObject] {
//Passing the key Top(Array) here and defining it from plist
if let drug = top["Drugs"] as? [AnyObject] {
return drug
} else {
return [AnyObject]()
}
}
var heads: [AnyObject] {
//Passing the key Top(Array) here and defining it from plist
if let heads = top["Head"] as? [AnyObject] {
return heads
} else {
return [AnyObject]()
}
}
override func viewDidLoad() {
super.viewDidLoad()
//Assigning the key Top here to title
if let name = top["Top"] as? String {
title = name
}
tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: CellIdentifier)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
return heads.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return subheads.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let titleName = title
if let titleHeader = heads as? [String: AnyObject], let titled = titleHeader["Headings"] as? String
{
titleName.text = titled
}
return titleName
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Dequeue Resuable Cell
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath)
if let head = heads[(indexPath as NSIndexPath).row] as? [String: AnyObject], let title = head["Headings"] as? String {
//Topics is the Key inside array top
// Configure Cell
cell.textLabel?.text = title
}
return cell;
}
}
The plist file is a bit big and goes like this.
<plist version="1.0">
<array>
<dict>
<key>Chap</key>
<string>ANS</string>
<key>Top</key>
<array>
<dict>
<key>Topics</key>
<string>Cholinergics</string>
<key>Head</key>
<array>
<dict>
<key>Headings</key>
<string>Directly acting</string>
<key>Subhead</key>
<array>
<dict>
<key>Subheadings</key>
<string>Choline esters </string>
<key>Drugs</key>
<array>
<string>Acetylcholine</string>
<string>Bethanechol</string>
<string>Carbhachol</string>
<string>Methacholine</string>
</array>
</dict>
<dict>
<key>Subheadings</key>
<string>Natural Alkaloids</string>
<key>Drugs</key>
<array>
<string>Nicotine</string>
<string>Pilocarpine</string>
<string>Muscarine</string>
<string>Arecoline</string>
</array>
</dict>
</array>
</dict>
<dict>
<key>Headings</key>
<string>Indirectly acting</string>
<key>Subhead</key>
<array>
<dict>
<key>Subheadings</key>
<string>Reversible Carbamates</string>
<key>Drugs</key>
<array>
<string>Neostigmine</string>
<string>Edrophonium</string>
<string>Physostigmine</string>
<string>Pyridostigmine</string>
<string>Ambenonium</string>
<string>Galantamine</string>
<string>Donepezil</string>
<string>Rivastigmine</string>
</array>
</dict>
<dict>
<key>Subheadings</key>
<string>Reversible Acridine</string>
<key>Drugs</key>
<array>
<string>Tacrine</string>
</array>
</dict>
<dict>
<key>Subheadings</key>
<string>Irreversible Carbamates</string>
<key>Drugs</key>
<array>
<string>Propoxur</string>
</array>
</dict>
<dict>
<key>Subheadings</key>
<string>Irreversible organophosphates</string>
<key>Drugs</key>
<array>
<string>Echothiophate</string>
<string>Parathion</string>
<string>Malathion</string>
<string>Dyflos</string>
<string>Diazinon</string>
<string>Tabun</string>
<string>Sarin</string>
<string>Soman</string>
</array>
</dict>
</array>
</dict>
<dict>
<key>Headings</key>
<string>Cholineesterase Reactivators</string>
<key>Drugs</key>
<array>
<string>Pralidoxime</string>
<string>Obidoxime</string>
<string>Trimedoxime</string>
</array>
</dict>
</array>
</dict>
<dict>
Your variable heads
is an array. So you should get the element with the index of section
from heads
array and then get "Headings" from the titleHeader
.
Try this
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
var titleName = title
if let titleHeader = heads[section] as? [String: AnyObject], let titled = titleHeader["Headings"] as? String
{
titleName = titled
}
return titleName
}