我有一个如下所示的手风琴菜单示例,我想制作一个嵌套菜单(例如菜单中的菜单),这主要与可扩展的 tableViews 相关,但我需要在可扩展的 tableView 或任何其他解决方案中进行扩展,这里的代码来自做单步手风琴的互联网,我需要嵌套的: PS:我的项目有点重,所以我不想添加其他库,也许只是一个类,非常感谢你提前
// Created by ingdanni on 05/11/15.
// Copyright (c) 2015 ManaguaIO. All rights reserved.
//
import UIKit
struct Section {
let title: String
let rows: [String]
var selected: Bool
}
class ViewController: UIViewController {
let CellIdentifier = "Cell"
var sections = [Section]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Setup Sections
sections.append(Section(title: "A", rows: ["1","2","3","4"], selected: false))
sections.append(Section(title: "B", rows: ["5","6","7","8"], selected: false))
sections.append(Section(title: "C", rows: ["9","10"], selected: false))
// Set cell reuse identifier
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: CellIdentifier)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if sections[section].selected
{
return sections[section].rows.count
}
return 0
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return ""
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 1
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if sections[indexPath.section].selected {
return 50
}
return 2
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
headerView.backgroundColor = UIColor.lightGrayColor()
headerView.tag = section
let headerString = UILabel(frame: CGRect(x: 10, y: 10, width: tableView.frame.size.width-10, height: 30)) as UILabel
headerString.text = sections[section].title
headerView.addSubview(headerString)
let headerTapped = UITapGestureRecognizer(target: self, action:"sectionHeaderTapped:")
headerView.addGestureRecognizer(headerTapped)
return headerView
}
func sectionHeaderTapped(recognizer: UITapGestureRecognizer) {
let indexPath = NSIndexPath(forRow: 0, inSection:(recognizer.view?.tag as Int!)!)
if indexPath.row == 0 {
sections[indexPath.section].selected = !sections[indexPath.section].selected
//reload specific section animated
let range = NSMakeRange(indexPath.section, 1)
let sectionToReload = NSIndexSet(indexesInRange: range)
self.tableView.reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade)
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath)
cell.textLabel?.text = sections[indexPath.section].rows[indexPath.row]
return cell
}
}