Veronica G Veronica G - 1 year ago 64
Swift Question

TableView segue with multipe arrays

I have a table view with two sections, both calling two different arrays.

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

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


How can I pass the information of both through a segue?

This is my information for the segue, "Data" is a struct on a seperate file.

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


But I have two arrays, and I'm not sure how to go about it. Also, this information doesn't work. It says the passed information is nil and my app crashes. Both arrays have information append to it.

This is the whole 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

}
}

}


The information on my arrays is this...

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)


And so on...

Answer Source

In the table view controller:

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 the second view controller, have:

var data = Data()

Then use the information from data to fill in your labels and such:

override func viewDidLoad() {

    super.viewDidLoad()

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