0

How do I pass data between two table views? I have poet.plist with 2 arrays: name and biography. When I run the app, second table view is empty. The first table view should list poets while the second table view lists works of poets.

First class

class Poets
{
    var poetName:String = ""
    var poetImage:String = ""
    var books=[Works]()
}

Second Class:

class Works {

    var nameWork = ""
    var workPoet = ""

    init(nameWork: String, workPoet: String) {
        self.nameWork = nameWork
        self.workPoet = workPoet
    }
}

First View Controller

let worksArray = booksDict[poet.poetName as String] as! NSArray
for work in worksArray {
    let dictionaryFromArray = work as! NSDictionary
    let workObject = Works(nameWork: dictionaryFromArray["nameWork"] as! String, workPoet: dictionaryFromArray["work"] as! String)
    poet.books.append(workObject)
}

How can I make a segue between the UITableViewController with names of poets and the UITableViewController with works of poets?

Brian
  • 14,610
  • 7
  • 35
  • 43
Diana
  • 683
  • 1
  • 8
  • 17
  • 1
    in `WorksTableViewController` the function `numberOfRowsInSection` should return `biography.count`, `numberOfSectionsInTableView` should return 1 – vadian Oct 08 '15 at 14:24
  • @vadian Thank you.But I think I made a mistake with that code at all. I don't understand how to make by clicking on the row of the first table was the second table with the list. The data should be in plist. For example,the first table:the machines-the second table-the model of the machine. It's dictionary with arrays?How can i do it? – Diana Oct 08 '15 at 14:32
  • Declaring the model variables outside a class is bad habit. Create a new file and declare a custom class `Poet` with properties `name`, `yearsOfLife`, `image` and `biography`. In `PoetsTableViewController` create a variable `poets = [Poet]()` . When reading the plist file, create `Poet` instances, populate the properties from `dict` and add the instances to `poets`. In `prepareForSegue` pass the `Poet` instance depending on the index path. In the `WorksTableViewController` populate the cells with the information in the `Poet` instance. – vadian Oct 08 '15 at 14:40
  • PS: now my comments are confusing because you removed all related information in your question – vadian Oct 08 '15 at 14:48
  • @vadian Thank you I'm a little edit the question. I tried to arrange everything as you write in the comments , but I do not understand , how passing data between table view controllers – Diana Oct 08 '15 at 14:50
  • There are many tutorials in the web, for example Ray Wenderlich is quite popular for his comprehensible tutorials. Look for a Master - Detail table view application. – vadian Oct 08 '15 at 14:53
  • Possible duplicate: [Passing Data between View Controllers](http://stackoverflow.com/q/5210535/643383) – Caleb Oct 08 '15 at 16:13

1 Answers1

2

In 1st View Controller

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    // ShowPoetWorks is your segue identifier which you can set in storyboard
    if segue.identifier == "ShowPoetWorks" {

        var secondVC = segue.destinationViewController as? SecondViewController
        secondVC.poetName = poetName
    }
}

In 2nd View Controller

  • Define 2 field: poetName and workCount
  • Override function `tableView:numberOfRowsInSection

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        return workCount  
    }
    
  • In viewDidLoad, load poet works base on poetName and call tableView.reloadData()
truongky
  • 1,147
  • 9
  • 12
  • you mean *How can I make a segue between the UITableViewController with names of poets and the UITableViewController with works of poets?*? Drag and drop from poets UITableViewController to works UITableViewController. – truongky Oct 09 '15 at 10:42