0

I have an array that looks like:

[Category(title: "First Category", question: [
     Question(
 title: "1 Question",
 article: "1 Arcticle",
 anwser: "Answer",
 link: "google.com",
 favorite: false,
 possibleAnswers: ["First", "Second", "Third", "Fourth"],
 rightAnswerNumber: 2), etc..]),
etc..]

I don't understand how to extract data in next ViewController. Here I have a code In parent:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == ListViewController.className() {
        let indexPath = tableView.indexPathForSelectedRow
        let destinationVC = segue.destination as? ListViewController
        var data: Category
        if isFiltering() {
            data = filteredData[indexPath!.row]
        } else {
            data = dataSource[indexPath!.row]
        }
        destinationVC?.passedData = data
    }
}

In child:

var passedData: Category?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: ListTableViewCell.identifier(), for: indexPath) as! ListTableViewCell
    cell.passedData = passedData![indexPath.row][indexPath.row] //Here
    cell.selectionStyle = .gray
    return cell

}

I need to make cell.passedData take an array of Question in Category depends from indexPath, but don't know how.

I was thinking about extracting array from Category using for loop, but didn't realize how.

4
  • what is the type of cell.passedData? Commented Sep 9, 2018 at 14:32
  • I am passing exact single Category because I need to display title of Category and have array of Question to display in TableView. In first VC I have an array of Categories that same load titles in TableView and depends on selection passing data of Category (title, array of Questions) in next VC. Commented Sep 9, 2018 at 14:33
  • Is cell.passedDataof type Question or String? Commented Sep 9, 2018 at 14:38
  • cell.passedData is Question Commented Sep 9, 2018 at 14:42

1 Answer 1

1

Since passedData of the child view controller has a type of Category, you just need to replace

cell.passedData = passedData![indexPath.row][indexPath.row] //Here

with

cell.passedData = passedData!.question[indexPath.row]

in the child view controller

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Such a simple, my fault that I didn't realize that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.