UITable View:制作单独的部分并添加标题和副标题

时间:2017-01-02 12:10:54

标签: ios arrays swift swift3 plist

每个标题的单独部分; 在每个部分标题下应该是SubHeadings,副标题应该是Drugs。

我能够正确获取numberOfSections,如何设置正确的部分标题并为此项目调用标题和副标题?

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;
}
}

plist文件有点大,就像这样。

<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>

1 个答案:

答案 0 :(得分:0)

您的变量heads是一个数组。因此,您应该从section数组中获取索引为heads的元素,然后获取&#34;标题&#34;来自titleHeader

试试这个

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
    }