I have an example of accordion menu like below, and I want o make a nested menu, (e.g. the menu within menu) which is mostly related to expandable tableViews, but I need expandable within expandable tableView or any other solution here is the code from internet that does the single step accordion which I need the nested one: PS: my porject is a bit heavy so I don't want to add other libraries, maybe just a class, thank you very much in advance
// 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
}
}
Actually after struggling a lot on this on how to make nested accordion I found my answer but then decide not to use it for dynamic data from server and JSON web service (e.g when you have nested tree menu which is has dynamic structure from the server) thus I have used each category in one page and define three level and use segues, but If you need to make such menu I found the trick in making an NSObject as for defining properties of each cell here it is:
and here is the viewController:
I specially thanks the user anoopm and I lost the link to repository anyone has it is good to put it in here, thanks by the way for reading this