2016-08-05 32 views
1

我有兩個部分的表視圖,都調用兩個不同的數組。TableView繼承multipe數組

var data1 = [Data]() 
var data2 = [Data]() 

let section = ["Section1", "Section2"] 

如何通過segue傳遞兩者的信息?

這是我爲segue提供的信息,「Data」是一個單獨的文件上的結構。我有兩個數組,但我不知道如何去做。此外,這些信息不起作用。它說傳遞的信息是零,我的應用程序崩潰。兩個數組都有信息附加到它。

這是整個SEGUE:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    if segue.identifier == cellIdentifier { 

    let destination = segue.destinationViewController as! DetailsViewController 

    if let indexPath = self.tableView.indexPathForSelectedRow { 

     let selectedInfo = data1[indexPath.row] 

     destination.detailsTitle.text = selectedInfo.dataTitle 
     destination.detailsImage.image = selectedInfo.dataImage 
     destination.detailsInfo.text = selectedInfo.dataDetails 
     destination.detailsGenre.text = selectedInfo.dataGenre 

     } 
    } 

} 

在我的陣列中的信息是這樣的......

let pic1 = UIImage(named: "killlakill") 
    var animeInfo = Data(title: "Kill la Kill", image: pic1!, details: "The story is set on a high school that the student council president Satsuki Kiryuuin rules by force. Wielding a giant Basami scissors sword, the wandering transfer student Ryuuko Matoi brings about upheaval on the campus. Ryuuko searches for the mysterious figure who caused her father's death, but confronting her are the student council's four divine kings. Fortunately, Ryuuko is aided by a talking sailor uniform who tells her, Wear me. When I am worn by you, this power will become manifest.", genre: "School, Comedy, Action", episodes: "24") 
    data1.append(animeInfo) 

等等......

+0

Data1中有data1 [indexPath.row]。這是一個錯字嗎? – Shades

+0

是的,它的錯字在這裏,對不起...它不看起來像xcode – art3mis

+0

我不完全得到發生了什麼事情。你如何獲得indexPath.row?什麼是確切的錯誤和地點?看來你需要將值賦給第二個視圖控制器中的實際變量,而不是分配給標籤本身。 – Shades

回答

1

在表視圖控制器:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 

    if let row = self.tableView.indexPathForSelectedRow?.row { 

     if let section = self.tableView.indexPathForSelectedRow?.section { 

      let destination = segue.destinationViewController as! DetailsViewController 

      if section == 0 { 
       let selectedInfo = data1[row] 
       destination.data = selectedInfo 
      } 
      else if section == 1 { 
       let selectedInfo = data2[row] 
       destination.data = selectedInfo 
      }  
     } 
    } 
} 

In第二個視圖控制器,具有:

var data = Data() 

然後利用這些信息從data填寫你的標籤,例如:

override func viewDidLoad() { 

    super.viewDidLoad() 

    detailsTitle.text = data.dataTitle 
    detailsImage.image = data.dataImage 
    detailsInfo.text = data.dataDetails 
    detailsGenre.text = data.dataGenre 
} 
+0

on var selectedInfo = Data()它說「不能調用初始值設定項對於沒有參數的類型數據」 – art3mis

+0

@VeronicaG查看更正 – Shades

+0

仍然給我同樣的錯誤。一切看起來不錯,但那一部分。 – art3mis

0

數據屬性更改爲:

let data = [Data, Data] 

然後在您的繼續使用中:

let selectedInfo = data[indexPath.section][indexPath.row]