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 :
Segunda Table :
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
– Gabriel Rodrigues
@Gabrielrodrigues, there’s the whole code. Will help?
– Raphael Albertini
You can put JSON too?
– Juan de Dios
I figured out what was wrong. Then, if you want, I put the answer! Vlw
– Raphael Albertini
Please, I could put the answer?
– peterq