2014-10-07 62 views
0

名爲行成員我有這樣的代碼:NSIndexPath不具有迅速

class ViewController: UITableViewController { 

    var myArr = [(id:1, name:"1123", description:"d1", image:"palm-tree.png"), (id:2, name:"23", description:"d2", image:"palm-tree.png"), 
       (id:3, name:"3", description:"d3", image:"palm-tree.png"), (id:4, name:"4", description:"d4", image:"palm-tree.png")] 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "showDetail" { 
      var indexPath = tableView.indexPathForSelectedRow() 
      let destinationViewController:DetailViewController = segue.destinationViewController as DetailViewController 
      destinationViewController.myArr = myArr[indexPath.row] 
     } 
    } 

} 

而且我有一個錯誤NSIndexPath沒有一個成員命名的行。如何解決這個問題?

謝謝!

更新:

class ViewController: UITableViewController { 
    struct Item { 
     let id: Int 
     let name: String 
     let description: String 
     let image: String 
    } 

    var myArr = [Item(id:1, name:"1123", description:"d1", image:"palm-tree.png"), 
     Item(id:2, name:"23", description:"d2", image:"palm-tree.png"), 
     Item(id:3, name:"3", description:"d3", image:"palm-tree.png"), 
     Item(id:4, name:"4", description:"d4", image:"palm-tree.png")] 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "showDetail" { 
      if let indexPath = tableView.indexPathForSelectedRow() { 
       let destinationViewController = segue.destinationViewController as DetailViewController 
       destinationViewController.myArr = [myArr[indexPath.row]] 
      } 
     } 
    } 

} 

而且DetailViewController

class DetailViewController: UIViewController { 
    struct Item { 
     let id: Int 
     let name: String 
     let description: String 
     let image: String 
    } 
    var myArr = [Item(id:1, name:"1123", description:"d1", image:"palm-tree.png")] 
} 

回答

2

tableView.indexPathForSelectedRow()返回一個可選NSIndexPath - 你需要解開它訪問row屬性:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "showDetail" { 
     if let indexPath = tableView.indexPathForSelectedRow() { 
      let destinationViewController = segue.destinationViewController as DetailViewController 
      destinationViewController.myArr = [myArr[indexPath.row]] 
     } 
    } 
} 
+0

**'Int'不能轉換爲'Range '** – tim 2014-10-07 17:27:21

+0

看起來像你的'DetailViewController'正在等待一個數組 - 我已經更新了答案,所以它將得到一個包含當前行數據的單元數組'myArr'。 (這是不好的變量名稱,是描述性的!) – 2014-10-07 17:29:44

+0

對不起,我的錯誤 - 我忘了'DetailViewController':var myArr:(id:Int,name:String,description:String,img:String)=( id:0,name:「」,description:「」,img:「」) – tim 2014-10-07 17:36:42