Talk the guys! It is difficult to get the value of a array
in a uitableview
and move to another uitableview
.
There is no error appearing, but it does 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
}
}