我有一个可扩展的表视图,并用静态数组填充它,并且工作正常,但如果我想用NSDictionary或NSarrays填充此表,那么我该怎么做!?
这是我第一次使用可扩展的表视图,所以请指导我。
这是我的整个代码:
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]]()