简体   繁体   English

TableView单元格动作

[英]TableView Cell actions

I have a ViewController, with a list of data from a son file. 我有一个ViewController,其中包含子文件中的数据列表。 This page run perfectly. 此页面运行完美。 On this page are many pub's with prices. 在此页面上有很多酒吧的价格。 And I want to make another scene (SecondViewController). 我想制作另一个场景(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 请帮助我:D

Here is my ViewController: 这是我的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: 这是我的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: 这是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)

}
}

Instead of having the global variable myIndex , have a local variable in the second view controller. 在第二个视图控制器中有一个局部变量,而不是拥有全局变量myIndex Use prepare(for segue:) in the first view controller to assign the selected row index to that variable. 在第一个视图控制器中使用prepare(for segue:)将选定的行索引分配给该变量。


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: SecondViewController:

class SecondViewController: UIViewController {

    var myIndex: Int?

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

    // etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM