2016-11-22 44 views
0

我是新來的ios。 我想製作一個帶有可擴展菜單的導航抽屜,我該怎麼做。 我沒有導航抽屜,但現在想要在抽屜中添加子菜單,但我沒有得到如何做,請幫助。先謝謝你。如何使用可擴展的TableView或手風琴在Swift 3

這裏是我的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") 
    } 
} 

我有子菜單,「項目」,如添加項目,編輯項目,刪除項目代碼。 當用戶點擊項目我有展示這個子菜單,如手風琴, 和當用戶點擊任何子菜單(添加,編輯或刪除)它將打開在新的ViewController

回答

0
@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) 
} 
+0

對不起,但我有點混淆。我分配了這個var subMenuItems:[String] = [「Add」,「Update」,「Delete」] var menuToShow:[String] = menuItems在switch case中,因爲我有不同的子菜單。但我如何追加它們? – Pratik

+0

我能做些什麼來使它們可摺疊? – Pratik

+0

在當時點擊特定行時,您將subMenuItems分配給MenuTo用我已經提到的動畫顯示和重新加載部分。 –