0

Fala galera! To com dificuldade em pegar o valor de um array em uma uitableview e passar para outra uitableview. Não está aparecendo erro, mas não imprime nada no console quando acesso a próxima uitableview. A ideia é fazer uma 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
     }   
}
1
  • Oi, algumas perguntas para que eu possa ajudar. Vc já testou que os valores estão sendo carregados, correto? vc já colocou uma breakpoint para ver se o prepareForSegue está sendo chamado?
    – CCastro
    18/04/2016 às 20:37

1 Resposta 1

0

Você tem que pegar o toque na célula que você selecionou. Utilizando a storyboar você define o identificador da celula da tabela que esta usando para assim depois você colocar para a próxima view de destino.

    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

        }
    }

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .