2015-09-07 127 views
0

仍在迅速採取babysteps。Swift/iOs:如何鏈接動態添加的表格單元格?

對於靜態的tableview我只是CTRL +拖動表格單元向右導航控制器和選擇視圖顯示/場景。

但是,如果我使用了「動態原型」表視圖我如何能做到這一點?我像這樣填充表格視圖

let testMenu = [ 

     (「menuItem 1"), 
     ("menuItem test"), 
     (「afd"), 
     (「test"), 
     (「barca"), 
     (「london") 

    ] 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return testMenu.count 
    } 

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

     let cell = UITableViewCell() 
     let (menuItem) = testMenu[indexPath.row] 
     cell.textLabel?.text = menuItem 

     return cell 

    } 

這將按預期方式顯示菜單項。但是將每個表格單元鏈接到正確的導航控制器對我而言並不清楚。看一些教程,我想我需要使用didSelectRowAtIndexPath,但嘗試以下沒有打印任何內容,我不知道如何創建鏈接,segue?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    //CODE TO BE RUN ON CELL TOUCH 
    //need to go to the right navigation controller 

    print("clicked: \(indexPath)") 
} 

和類

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

EDIT 1:我能夠打印 「被點擊:{長度= 2,路徑= 0 - 1}」。在連接檢查器中,我必須拖動網點:代理 - >到視圖控制器

回答

0

您應該使用didSelectRowAtIndexPath在當前View Controller的導航控制器中推送正確的View Controller。

(對於您的信息,每個視圖控制器有一個內置navigationController)

使用方法,pushViewController()navigationController類的,你可以在當前視圖控制器的navigationController將任何視圖控制器,然後進一步處理視圖應該在推送的視圖控制器中完成。

如果你想推視圖控制器知道當前視圖控制器的表視圖的indexPath,只是使推視圖控制器,具有類型NSIndexPath的屬性自定義視圖控制器。

rightViewController對象的初始化,你會在推動過程中初始化在didSelectRowAtIndexPath方法(屬性)。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    //Before doing this, import the custom view controller that you want to push inside your navigation controller 
//1. Create the object of CustomRightViewController which is a subclass of UIViewController 
let rightViewController = CustomRightViewController(); 

//2. Set its myIndexPath property to selected index path , but before doing this, you must declare the myIndexPath property in your CustomRightViewController.swift file 
rightViewController.myIndexPath = indexPath; 

//3. Now you can push this Custom View Controller in your current View Controller's navigationController 
self.navigationController?.pushViewController(rightViewController, animated: true); 

//4. After this you can program the Custom View Controller accordingly, with the indexPath or any other object if you want to use from the current/initial View Controller. 

} 
+0

你能提供一些虛擬代碼? – alex

+0

我沒有太多熟悉的快捷,我是一個客觀的C語言開發,還是等待一段時間,我會揣摩...... –

+0

你可以看到上面的代碼,如果你發現任何更多的困難,PLZ在這裏評論 –

相關問題