Ios Don';我不知道如何用字典填充可扩展表视图

Ios Don';我不知道如何用字典填充可扩展表视图,ios,swift,tableview,expandable,Ios,Swift,Tableview,Expandable,我有一个可扩展的表视图,并用静态数组填充它,这很好,但是如果我想用NSDictionary或NSArray填充这个表,那么我应该怎么做 这是我第一次使用可扩展表视图,请指导我 这是我的全部代码: import UIKit class MenuViewController: UIViewController, UITableViewDataSource{ @IBOutlet weak var expandTableView: UITableView! var expandedSections

我有一个可扩展的表视图,并用静态数组填充它,这很好,但是如果我想用NSDictionary或NSArray填充这个表,那么我应该怎么做

这是我第一次使用可扩展表视图,请指导我

这是我的全部代码:

import UIKit

class MenuViewController: UIViewController, UITableViewDataSource{
@IBOutlet weak var expandTableView: UITableView!

var expandedSections : NSMutableSet = []
var sectionData : [String] = ["Vegs  ", "Fruits  ",  "Birds  ", "Reptiles  "]
var row1 = ["Tomato", "Onion", "Potato", "Pumpkin", "Babycorn", "Cucumber", "Carrot", "Beans", "Cabbage", "Corn", "EggPlant", "Pea", "Sweet Potato"]
var row2 = ["Mango", "Orange", "Watermelon", "Apple", "Apricot", "Banana", "Papaya", "Pineapple", "Melon", "Avocado", "Cherry", "Date", "Fig", "Grape", "Guava", "Kiwi", "Olive", "Pear"]
var row3 = ["Parrot", "Peacock", "Woodpecker", "Kingfisher", "Owl", "Eagle", "Pigeon", "Vulture", "Bats", "Nightingale", "Crow"]
var row4 = ["Lizard", "Crocodile", "Snake", "Turtle", "Dinosaur"]


func sectionTapped(_ sender: UIButton) {
    let section = sender.tag
    let shouldExpand = !expandedSections.contains(section)
    if (shouldExpand) {
        expandedSections.removeAllObjects()
        expandedSections.add(section)
    } else {
        expandedSections.removeAllObjects()
    }
    self.expandTableView.reloadData()
}
} // end of class 

extension MenuViewController: UITableViewDelegate  {


func numberOfSections(in tableView: UITableView) -> Int {
    return sectionData.count
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: 300, height: 28))
    var imageView = UIImageView()


    let headerTitle = UILabel.init(frame: CGRect(x: 38, y: 4, width: 250, height: 28))
    headerTitle.text = sectionData[section]
    headerTitle.textAlignment = .right


    let tappedSection = UIButton.init(frame: CGRect(x: 0, y: 0, width: headerView.frame.size.width, height: headerView.frame.size.height))
    tappedSection.addTarget(self, action: #selector(sectionTapped), for: .touchUpInside)
    tappedSection.tag = section

    headerView.addSubview(imageView)
    headerView.addSubview(headerTitle)
    headerView.addSubview(tappedSection)
    headerView.tintColor = UIColor.white
    return headerView
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 44
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(expandedSections.contains(section)) {
        switch section {
        case 0:
            return row1.count
        case 1:
            return row2.count
        case 2:
            return row3.count
        default:
            return row4.count
        }
    } else {
        return 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
    cell?.textLabel?.textAlignment = .right

    switch indexPath.section {
    case 0:
        cell?.textLabel?.text = row1[indexPath.row]
    case 1:
        cell?.textLabel?.text = row2[indexPath.row]
    case 2:
        cell?.textLabel?.text = row3[indexPath.row]
    default:
        cell?.textLabel?.text = row4[indexPath.row]
    }
    return cell!;
}
}
以下是我的新价值观:

var expandedSections : NSMutableSet = []
var categoryLevel1 = [Category]()
var categoryLevel2 = [[Category]]()

字典的值将是数组。钥匙是什么?它们与节的对应关系如何?我更新了我的问题。是否需要让每一行都包含一个文本视图,并且数组中的每一项都在一行上?是的,我希望将标题设置为父单元格