B.Boti
B.Boti

Reputation: 15

TableView Cell actions

I have a ViewController, with a list of data from a son file. This page run perfectly. On this page are many pub's with prices. And I want to make another scene (SecondViewController). And every time, when I push a pub from the list I want to display on another scene more information about that place. That run, but when I choose a pub the program shows the information about the first pub from the list, and when I choose another he shows the previous pub, which I choose before. And sorry my english is very bad. Please help me :D

Here is my ViewController:

import UIKit

var nev: [String] = []
var cim: [String] = []
var ar: [String] = []


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var myIndex: Int?

@IBOutlet weak var tableView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    let url = Bundle.main.url(forResource: "pubok", withExtension: "json")
    do {
        let allContactsData = try Data(contentsOf: url!)
        let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : AnyObject]
        if let arrJSON = allContacts["Pubok"] {
            for index in 0...arrJSON.count-1 {

                let aObject = arrJSON[index] as! [String : AnyObject]

                nev.append(aObject["Hely neve"] as! String)
                cim.append(aObject["Cím"] as! String)
                ar.append(aObject["Legolcsóbb sör"] as! String)
            }
        }

        self.tableView.reloadData()
    }
    catch {

    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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


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

    cell.nevLabel.text = nev[indexPath.row]
    cell.arLabel.text = ar[indexPath.row] + "/0.5l"
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    myIndex = indexPath.row
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let vc = segue.destination as! SecondViewController

    vc.myIndex = myIndex
}

}

Here is my SecondViewController:

import UIKit

class SecondViewController: UIViewController {

myIndex: Int?

@IBOutlet weak var secondnevLabel: UILabel!
@IBOutlet weak var secondcimLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    secondnevLabel.text = nev[myIndex!]
    secondcimLabel.text = cim[myIndex!]


}
}

And this is the TableViewCell:

import UIKit

class TableViewCell: UITableViewCell {


@IBOutlet weak var nevLabel: UILabel!
@IBOutlet weak var arLabel: UILabel!




override func awakeFromNib() {
    super.awakeFromNib()

}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

}
}

Upvotes: 0

Views: 70

Answers (1)

Shades
Shades

Reputation: 5616

Instead of having the global variable myIndex, have a local variable in the second view controller. Use prepare(for segue:) in the first view controller to assign the selected row index to that variable.


ViewController:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!


    override func viewDidLoad() {
    // etc
    }

    // Remove didSelectRowAt

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        let row = (self.tableView.indexPathForSelectedRow as NSIndexPath?)?.row

        let vc = segue.destination as! SecondViewController

        vc.myIndex = row
    }

SecondViewController:

class SecondViewController: UIViewController {

    var myIndex: Int?

    @IBOutlet weak var secondnevLabel: UILabel!
    @IBOutlet weak var secondcimLabel: UILabel!

    // etc

Upvotes: 1

Related Questions