如何使用可扩展的TableView或手风琴在Swift 3中

时间:2016-11-22 06:16:04

标签: ios swift

我是ios的新手。 我想制作一个带有可扩展菜单的导航抽屉,我该怎么办? 我做了导航抽屉,但现在想在抽屉里添加子菜​​单,但我没有得到怎么做,请帮忙。提前谢谢你。

这是我的DrawerViewController

代码
class LeftSideViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate {

var menuItems:[String] = ["Main","Project","Client","User","About","Logout"]

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return menuItems.count;
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let mycell = tableView.dequeueReusableCell(withIdentifier: "MenuItemCell", for: indexPath) as! MyCustomTableViewCell

    mycell.labelMenuItem.text = menuItems[indexPath.row]
    return mycell;
}

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    switch (indexPath.row) {
    case 0:

        let centerViewController = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController

        let centerNavController = UINavigationController(rootViewController: centerViewController)

        let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate

        appDelegate.centerContainer!.centerViewController = centerNavController
        appDelegate.centerContainer!.toggle(MMDrawerSide.left, animated: true, completion: nil)

        break;

    case 1:
        let aboutViewController = self.storyboard?.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController

        let aboutNavController = UINavigationController(rootViewController: aboutViewController)

        let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate

        appDelegate.centerContainer!.centerViewController = aboutNavController

        appDelegate.centerContainer!.toggle(MMDrawerSide.left, animated: true, completion: nil)


        break;

    default:
        print("\(menuItems[indexPath.row]) is selected")
    }   
}

我有“项目”的子菜单,例如添加项目,编辑项目,删除项目。 当用户点击Project我已经显示这个子菜单,比如手风琴, 当用户点击任何子菜单(添加,编辑或删除)时,它将以新的ViewController打开。

1 个答案:

答案 0 :(得分:-1)

@IBOutlet var tblViewMenu: UITableView!
var menuItems : NSMutableArray = ["dash","case","client"]
var subMenu : NSMutableArray = ["1","2"]
var menuToShow : NSMutableArray = []
override func viewDidLoad() {
    super.viewDidLoad()
    menuToShow = menuItems
    // Do any additional setup after loading the view, typically from a nib.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return menuToShow.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell")!
    let lblName : UILabel = cell.viewWithTag(1) as! UILabel
    lblName.text = menuToShow.objectAtIndex(indexPath.row) as? String
    return cell;
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    menuToShow = subMenu
    tableView.reloadSections(NSIndexSet.init(index: 0), withRowAnimation: UITableViewRowAnimation.Fade)
}