2015-11-20 48 views
0

我目前正在用Swift測試一下。 我想要做的是,製作一個帶有tableView的ViewController,並通過objc.But顯示它,當我添加init我的Swift類,並希望在我的objc viewcontroller中添加視圖作爲子視圖時,我的單元格隱藏一旦我觸摸桌面。在Objc中使用TableView的Swift ViewController使單元格隱藏

這是我的銀行代碼: MainMenuViewController.swift

import UIKit 

@objc 
class MainMenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{ 

    @IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.registerNib(UINib(nibName: "MainMenuTableViewCell", bundle: nil), forCellReuseIdentifier: "Cell"); 
     tableView.delegate = self; 
     tableView.dataSource = self; 
    } 

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

    //MARK : tableview 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1; 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1; 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MainMenuTableViewCell; 

     //cell.icon.image = UIImage(named: "clock") 
     cell.menuTitle.text = "test" 

     return cell; 
    } 
} 

MainMenuTableViewCell.swift

class MainMenuTableViewCell: UITableViewCell 
{ 
    //MARK : properties 

    @IBOutlet weak var menuTitle: UILabel! 
    @IBOutlet weak var icon: UIImageView! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 

,這是我怎麼加的觀點在ObjC: ViewController.m

- (IBAction)ShowMenu:(id)sender 
{ 
    MainMenuViewController *mainMenu = [[MainMenuViewController alloc] init]; 
    [mainMenu.view setFrame:CGRectMake(0,self.navigationController.navigationBar.height, self.view.width-100, self.view.height-self.navigationController.navigationBar.height)]; 
    [self.view addSubview:mainMenu.view]; 
} 

回答

0

嘗試將其添加爲子視圖控制器

[childController willMoveToParentViewController:rootViewController]; 
[rootViewController addChildViewController:childController]; 
[rootViewController.view addSubview:childController.view]; 
[childController didMoveToParentViewController:rootViewController]; 

希望這個作品

+0

似乎工作。但是爲什麼:D? – TdoubleG

+0

@TdoubleG在將子視圖添加到視圖層次結構之前,您的容器視圖控制器必須將子視圖控制器與其自身相關聯。這允許iOS將事件正確地路由到子視圖控制器以及這些控制器管理的視圖 –

相關問題