2016-08-05 34 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]