1

Estou com dificuldades em popular a minha UITableView com JSON. Separei algumas imagens para mostrar o problema. Notem que na primeira Table aparecem valores repetidos e nas próximas mostra o mesmo valor.

Primeira Table :

inserir a descrição da imagem aqui

Segunda Table :

inserir a descrição da imagem aqui

A estrutura que estou querendo montar é igual ao app kekanto. Ou seja, na primeira view teria que aparecer os sobrenomes (sem repetir), na segunda view, os nomes das pessoas referentes ao sobrenomes selecionado e na última view, deveria aparecer os detalhes da pessoas selecionada.

Ou seja: familia> pessoa > detalhe

Segue o código :

Model

import Foundation

class MyLocationModel: NSObject {

    //properties
    var id: String?
    var familia_id : String?
    var nome: String?
    var familia: String?
    var filhos : String?

    //empty constructor

    override init()
    {

    }


    init(id: String, familia_id: String, nome: String, familia: String, filhos: String) {

        //self.id = id
        //self.familia_id = familia_id
        //self.nome = nome
        self.familia = familia
        //self.filhos = filhos

    }


    //prints object's current state

    override var description: String {
        return "ID: \(id), NOME: \(nome), FAMILIA: \(familia), FILHOS: \(filhos)"

    }


}

    import Foundation

class localPessoasModelo: NSObject {

    //properties
    var id: String?
    var familia_id : String?
    var nome: String?
    var familia: String?
    var filhos : String?

    //empty constructor

    override init()
    {

    }


    init(id: String, familia_id: String, nome: String, familia: String, filhos: String) {

        //self.id = id
        self.familia_id = familia_id
        self.nome = nome
        //self.familia = familia
        //self.filhos = filhos

    }


    //prints object's current state

    override var description: String {
        return "ID: \(id), NOME: \(nome), FAMILIA: \(familia), FILHOS: \(filhos)"

    }


}

TableViewController:

import UIKit

class ViewController: UITableViewController, modeloProtocal  {

    //Properties

    var feedItems: NSArray = NSArray()
    var selectedLocation : MyLocationModel = MyLocationModel()



    @IBOutlet weak var listTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        //set delegates and initialize homeModel

        self.listTableView.delegate = self
        self.listTableView.dataSource = self

        let Model = modelo()
        Model.delegate = self
        Model.downloadItems()

    }


    func itemsDownloaded(items: NSArray) {

        feedItems = items
        self.listTableView.reloadData()

    }


    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


        return feedItems.count

    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cellIdentifier: String = "Cell"
        let myCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)! as UITableViewCell

        let item: MyLocationModel = feedItems[indexPath.row] as! MyLocationModel

        myCell.textLabel!.text = item.familia

        return myCell

    }

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

        // Get the new view controller using [segue destinationViewController].
        let segundaScene = segue.destinationViewController as! segundaTableViewController

        // Pass the selected object to the destination view controller.
        if let indexPath = self.listTableView.indexPathForSelectedRow {
            let row = Int(indexPath.row)+1
            //detailScene.objetoAtual = feedItems[row] as? MyLocationModel
            print(row)

            segundaScene.selectRow = row


        }

    }
}

Segunda TableViewController:

import UIKit
class segundaTableViewController: UITableViewController, modeloPessoasProtocal {

    var listaArray : NSArray = NSArray()    
    var selectPessoas : localPessoasModelo?


    var selectRow = Int()

    @IBOutlet var segundaTableView: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()

        self.segundaTableView.delegate = self
        self.segundaTableView.dataSource = self

        print(selectRow)


    }

    func itemsDownloaded(items: NSArray) {

        listaArray = items
        self.segundaTableView.reloadData()

    }



    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return listaArray.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cellIdentifier: String = "segundaCell"
        let myCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)! as UITableViewCell

        myCell.textLabel!.text = selectPessoas?.nome

        return myCell

    }   
}

Detail View Controller:

import UIKit

class DetailViewController: UIViewController {

    var objetoAtual : MyLocationModel?

    var myArray : NSMutableArray = []

    @IBOutlet weak var myLabel: UILabel!

    @IBOutlet weak var nomeLabel: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()


        if let object = objetoAtual {
            myLabel.text = object.familia
            nomeLabel.text = object.nome
        }
    }

}

Você pode visualizar mais detalhes do projeto clicando aqui e aqui.

5
  • Poste o código também, só as fotos fica complicado 13/04/2016 às 20:57
  • @GabrielRodrigues, tá aí o código todo. Será que assim ajuda? 14/04/2016 às 12:29
  • Você pode colocar o JSON também? 19/04/2016 às 17:35
  • eu descobri o que estava de errado. Depois, se quiserem, eu posto a resposta! Vlw 20/04/2016 às 22:21
  • 1
    Por favor, teria como colocar a resposta?
    – peterq
    21/09/2016 às 15:33

2 Respostas 2

0

acho que assim pode ajudar e melhorar teu código.

ViewController

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

var dataSource: [MyLocationModel]!

override func viewDidLoad() {
    super.viewDidLoad()

    loadData()
    setupTable()
}

func loadData() {
    //carregar seu json
}

func setupTable() {
    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.tableView.tableFooterView = UIView()
    self.tableView.reloadData()
}
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dataSource.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell

    let item = dataSource[indexPath.row]

    cell.textLabel?.text = item.familia

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //
}
}
0

Na sua Segunda tableViewController você não adicionou o index [indexPath.row]

//Altere isso

      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {

    let cellIdentifier: String = "segundaCell"

    let myCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)! as UITableViewCell
    myCell.textLabel!.text = selectPessoas?.nome
    return myCell
} 

//para isso

    override func tableView(tableView: UITableView, cellForRowAtIndexPath 
    indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier: String = "segundaCell"
    let myCell: UITableViewCell = 
    tableView.dequeueReusableCellWithIdentifier(cellIdentifier)! as 
    UITableViewCell
    let pessoa = selectPessoas[indexPath.row]
    myCell.textLabel!.text = pessoa?.nome

    return myCell

} 

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 .