Uitableviewcontroller with Swift and JSON Displaying repeated values

Asked

Viewed 190 times

1

I’m having a hard time getting popular with mine UITableView with JSON. I separated some images to show the problem. Note that in the first Table Repeated values appear and next shows the same value.

First Table :

inserir a descrição da imagem aqui

Segunda Table :

inserir a descrição da imagem aqui

The structure I’m trying to build is the same as app kekanto. That is, in the first view would have to appear the surnames (without repeating), in the second view, the names of the persons referring to the selected surnames and in the last view, the details of the selected persons should appear.

That is to say: familia> pessoa > detalhe

Follows 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.

  • Post the code too, only the photos get complicated

  • @Gabrielrodrigues, there’s the whole code. Will help?

  • You can put JSON too?

  • I figured out what was wrong. Then, if you want, I put the answer! Vlw

  • 1

    Please, I could put the answer?

2 answers

0

In your Second tableViewController you have not added the index [indexPath.Row]

//Change that

      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
} 

//for that reason

    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

} 

0

I think it might 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) {
    //
}
}

Browser other questions tagged

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