2016-08-05 10 views
1

2つのセクションを持つテーブルビューがあり、どちらも2つの異なる配列を呼び出します。マルチビュー配列を持つTableView segue

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

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

どのようにしてセグの情報を渡すことができますか?

これは私の情報です。「データ」は別ファイルの構造体です。

let destination = segue.destinationViewController as! DetailsViewController 

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 

しかし、私は2つの配列を持っていますが、どうすればよいか分かりません。また、この情報は機能しません。渡された情報はゼロで、私のアプリケーションはクラッシュします。どちらの配列にも情報が付加されています。

これは全体セグエです:私のアレイ上

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を取得していますか?正確なエラーとは何ですか? 2つ目のビューコントローラで実際の変数に値を代入する必要があるようですが、ラベル自体に代入する必要はありません。 – 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 
      }  
     } 
    } 
} 

第2のビューコントローラ、持っている:

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

をvar selectedInfo = Data()にすると、 "引数なしの型データのイニシャライザを呼び出せません" – art3mis

+0

@VeronicaG修正を参照 – Shades

+0

は私に同じエラーを与えます。すべてが良く見えるが、その部分。 – art3mis

0
にデータプロパティを変更し

:その後、

let data = [Data, Data] 

そして、あなたのセグの使用:

let selectedInfo = data[indexPath.section][indexPath.row] 
関連する問題