Pass array from one Uitableview to another Uitableview

Asked

Viewed 136 times

0

Talk guys! I’m having a hard time getting the value of a array in a uitableview and move on uitableview. No error appears, but do not print anything on the console when accessing the next uitableview. The idea is to make a lista > lista > DetailView

import UIKit

class KivaLoanTableViewController: UITableViewController {

    let kivaLoadURL = "http://localhost/codeGil/service.php"
    var lista = [Lista]()


    func getLatestLoans() {
        let request = NSURLRequest(URL: NSURL(string: kivaLoadURL)!)
        let urlSession = NSURLSession.sharedSession()
        let task = urlSession.dataTaskWithRequest(request, completionHandler: {
            (data, response, error) -> Void in

            if let error = error {
                print(error)
                return }

            // Parse JSON data
            if let data = data {
                self.lista = self.parseJsonData(data)

                // Reload table view
                NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                    self.tableView.reloadData()
                })
            }
        })

        task.resume()
    }

    func parseJsonData(data: NSData) -> [Lista] {

        var lis = [Lista]()

        do {
            let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary

            // Parse JSON data
            let jsonLoans = jsonResult?["Familias"] as! [AnyObject]
            for jsonLoan in jsonLoans {
                let lista = Lista()
                lista.familia = jsonLoan["familia"] as! String

                let jsonMembros = jsonLoan["membros"] as! [AnyObject]
                for jsonMembro in jsonMembros {
                    let membros = Lista()
                    membros.nome = jsonMembro["nome"] as! String
                    membros.idade = jsonMembro["idade"] as! String
                    membros.sexo = jsonMembro["sexo"] as! String
                }
                lis.append(lista)
            }
        } catch {
            print(error)
        }

        return lis
    }



    override func viewDidLoad() {
        super.viewDidLoad()

        getLatestLoans()

    }


    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return lista.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! KivaLoanTableViewCell

        cell.familiaLabel.text = lista[indexPath.row].familia
        cell.sexoLabel.text = lista[indexPath.row].sexo
        cell.nomeLabel.text =  lista[indexPath.row].nome
        cell.idadeLabel.text = lista[indexPath.row].idade


        return cell
    }



     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
        let DestViewController = segue.destinationViewController as! SecondTableViewController

        let teste : String = lista[indexPath.row].nome
        DestViewController.segundaTabelaStr = teste
     }   
}
  • Hi, a few questions so I can help. You have already tested which values are being loaded, correct? you have already put a breakpoint to see if prepareForSegue is being called?

1 answer

0

You have to tap the cell you selected. Using the storyboar you define the cell identifier of the table you are using for so then you put to the next target view.

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "indetificador_segue" {

            let DestViewController = segue.destinationViewController as! SecondTableViewController
            let teste : String = lista[indexPath.row].nome

            // Lembre-se de declarar o [segundaTabelaStr] na outra view controller
            DestViewController.segundaTabelaStr = teste

        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.