UITableViewController with Swift and JSON Displaying repeated values

1

I'm having trouble popularizing my UITableView with JSON . I separated some pictures to show the problem. Note that in the first% w_ of% repeated values appear and in the next shows the same value.

First Table :

SecondTable:

ThestructureI'mtryingtobuildisequaltoTable kekanto . That is, the first app would have to appear the surnames (without repeating), in the second view , the names of the people referring to the surnames selected and in the last view , the details of the selected people should appear. >

That is: view

Follow the code:

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


        }

    }
}

Second 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
        }
    }

}

You can view more details of the project by clicking here and here .

    
asked by anonymous 13.04.2016 / 22:42

1 answer

0

I think it can help and improve your code.

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) {
    //
}
}
    
11.10.2018 / 17:52