我有一个包含两个部分的表视图,它们都调用两个不同的数组。
var data1 = [Data]()
var data2 = [Data]()
let section = ["Section1", "Section2"]
如何通过segue传递两者的信息?
这是我对segue的信息,“Data”是一个单独文件的结构。
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
但我有两个阵列,我不知道该怎么做。此外,此信息不起作用。它说传递的信息是零,我的应用程序崩溃。两个阵列都附加信息。
这是整个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 :(得分: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
}
}
}
}
在第二个视图控制器中,有:
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
}
答案 1 :(得分:0)
将您的数据属性更改为:
let data = [Data, Data]
然后在你的segue中使用:
let selectedInfo = data[indexPath.section][indexPath.row]