2015-11-13 71 views
0

我有兩個ViewController。對於第一個ViewController,它在表格上顯示我的Array數據。我想獲得所選單元格的indexPath,並將這些數據傳遞給另一個ViewController。將選定單元格傳遞給另一個ViewController

在我的第一個視圖控制器

import UIKit 

class ViewController: UIViewController, UITableViewDataSource { 
var nameList = [NameManager]() 

    @IBOutlet weak var NameTable: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     NameTable.dataSource = self 
     GetData() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func GetData(){ 
     let session = NSURLSession.sharedSession() 
     let request = NSMutableURLRequest(URL: NSURL(string: "http://www.json-generator.com/api/json/get/bPfifKWNaq?indent=2")!) 
     request.HTTPMethod = "GET" 

     let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in 

      if let error = error { 
       print(error) 
      } 
      if let data = data{ 
       do{ 
        let resultJSON = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) 
        let resultArray = resultJSON as? NSArray 
        for jsonObjectString in resultArray!{ 
         let code = jsonObjectString["code"] as! String 
         let name = jsonObjectString["name"] as! String 
         let description = jsonObjectString["description"] as! String 
         self.nameList.append(NameManager(code: code, name: name, description: description)) 
        } 
        self.nameList.count 
        dispatch_async(dispatch_get_main_queue(), { 
         self.NameTable.reloadData() 
        }) 
       }catch _{ 
        print("Received not-well-formatted JSON") 
       } 

      } 
      if let response = response { 
       let httpResponse = response as! NSHTTPURLResponse 
       print("response code = \(httpResponse.statusCode)") 
      } 

     }) 
     task.resume() 

    } 

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
     let myCell = NameTable.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell 

     myCell.textLabel?.text = nameList[indexPath.row].name 
     myCell.detailTextLabel?.text = nameList[indexPath.row].description 

     return myCell 
    } 

在我的第二個視圖控制器,這也是另一個類,我想捕捉選擇什麼樣的indexPath。使用索引值,我搜索我的數組並將該特定對象傳遞給下一個類。我沒有代碼,因爲我不知道它是如何工作的。

+0

如何顯示兩個控制器?你在第一個單元格選擇後顯示第二個? – Eluss

+0

使用導航控制器,單擊單元格時,它會將我帶到下一個控制器。兩者使用不同的課程。 –

回答

0

您可以在您的課程範圍之外創建一個屬性,然後在cellForRowAtIndexPath方法中設置該屬性。該屬性可以在您的第二個視圖控制器中訪問。您將需要查看tableview方法didSelectRowAtIndexPath,因爲這將是您將屬性設置爲所選單元格的位置。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
appDelegate().index = indexPath.row 
} 

我做了一個超級簡單的project on github顯示創建視圖控制器的範圍之外的財產的方法有兩種。一種方法是在AppDelegate內創建一個變量,通過一個單例訪問,另一個是Globals.swift文件。

希望這會有所幫助!

+0

對不起,我是新來的快速編程。所以通過創建一個屬性並在'didSelectRowAtIndexPath'處設置值,在我的另一個類中,我還必須實例化一個新的對象,這個對象在解壓時會返回爲null? –

+0

我在github上更新了這個項目(參見上面的鏈接)是一個將值從一個視圖控制器傳遞給另一個視圖控制器的簡單演示。 HTH – joeh

0

如果您想要將值傳遞給點擊單元格後彈出的控制器,使用單例將不是一種優雅的方式。如果你正在使用故事板,那麼你必須使用'準備繼續'。您可以在處理傳輸的類中實現該方法,並在另一個視圖控制器中設置所有屬性。

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 
    if (segue.identifier == "SecondVC") { 
     // set properties 
    } 
} 
相關問題