UitableView选择了单元格到另一个视图

时间:2017-05-10 12:47:10

标签: ios swift uitableview

我有一个UITableView,其中数据从数据库加载,即JSON。当我选择一行时,如何在另一个视图中拍摄?

要在tableview中选择自动跟踪并显示在另一个视图的标签中。

class AutoMarkeTableView: UITableViewController {
    var items = [[String:AnyObject]]()

    @IBOutlet var myTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "URL_LINK")!
        let urlSession = URLSession.shared

        let task = urlSession.dataTask(with: url) { (data, response, error) in
            // JSON parsen und Ergebnis in eine Liste von assoziativen Arrays wandeln
            let jsonData = try! JSONSerialization.jsonObject(with: data!, options: [])
            self.items = jsonData as! [[String:AnyObject]]

            // UI-Darstellung aktualisieren
            OperationQueue.main.addOperation {
                self.tableView.reloadData()
            }
        }

        task.resume()
    }

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

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

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

        let item = items[indexPath.row]

        cell.textLabel?.text = item["makename"] as? String

        return cell
    }
}

class FahrzeugAngabenView: UIViewController {
    @IBOutlet weak var itemMarkeLabel: UILabel!
}

View1

View2

2 个答案:

答案 0 :(得分:0)

现在他去了,但标签仍然是空的。

class AutoMarkeTableView: UITableViewController {

var items = [[String:AnyObject]]()

@IBOutlet var myTableView: UITableView!

override func viewDidLoad() {

    super.viewDidLoad()

    // set table view delegate and data source
    self.myTableView.delegate = self
    self.myTableView.dataSource = self

    let url = URL(string: "URL_LINK")!
    let urlSession = URLSession.shared

    let task = urlSession.dataTask(with: url) { (data, response, error) in

        // JSON parsen und Ergebnis in eine Liste von assoziativen Arrays wandeln
        let jsonData = try! JSONSerialization.jsonObject(with: data!, options: [])
        self.items = jsonData as! [[String:AnyObject]]

        // UI-Darstellung aktualisieren
        OperationQueue.main.addOperation {
            self.tableView.reloadData()
        }
    }

    task.resume()
}

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

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return self.items.count
}


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

    let item = items[indexPath.row]

    cell.textLabel?.text = item["makename"] as? String

    return cell
}

// go to details screen when clicked on a table row.
func tableView(tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "auto", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.

    if segue.identifier == "auto" {

        // find indexpath for selected row
        let selectedIndexPath = self.myTableView.indexPathForSelectedRow
        // deselet row
        self.myTableView.deselectRow(at: selectedIndexPath!, animated: true)

        // set title
        let selectedTitle = ""

        // create object for destination view controller
        let destVc = segue.destination  as! FahrzeugAngabenView

        destVc.selectedItemName = selectedTitle

    }
  }
}

class FahrzeugAngabenView: UIViewController {

var selectedItemName = "---"

@IBOutlet weak var itemMarkeLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    self.itemMarkeLabel.text = self.selectedItemName        
  }   
}

enter image description here

答案 1 :(得分:0)

您可以暂时将所选项目保存在变量中。像这样:

var selectedItem: Item?
func tableView(tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedItem = items[indexPath.row]
    self.performSegue(withIdentifier: "auto", sender: self)
}

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

    if segue.identifier == "auto" {
        let destVc = segue.destination  as! FahrzeugAngabenView

        destVc.selectedItemName = selectedItem.title
        selectedItem = nil
    }
 }

不是最优雅的解决方案,但我希望这可行。