简体   繁体   English

将数据从tableView传递到UIViewController swift 3

[英]Passing data from tableView to UIViewController swift 3

i'm trying to pass the info that are in every single cell to a UIViewController like you see in these images 我正在尝试将每个单元格中的信息传递给UIViewController,就像您在这些图像中看到的那样

enter image description here 在此处输入图片说明

this is the code of the UITableViewController: 这是UITableViewController的代码:

import UIKit
import MapKit
import CoreLocation

class CourseClass: UITableViewController, CLLocationManagerDelegate  {


    @IBOutlet weak var map: MKMapView!

    let manager = CLLocationManager()

    var place = ["Caffè Spagar", "Duks", "Posta station", "Barnum", "Elephant Club", "Cinema", "Space", "Andromeda", "Rodolf", "Devil Chair"]
    var rows = 0





    override func viewDidLoad() {
        super.viewDidLoad()

        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

    }



    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        insertRowsMode3()
    }


    func insertRowsMode2() {

        for i in 0..<place.count {
            insertRowMode2(ind: i, str: place[i])
        }

    }

    func insertRowMode2(ind:Int,str:String) {

        let indPath = IndexPath(row: ind, section: 0)

        rows = ind + 1
        tableView.insertRows(at: [indPath], with: .right)
    }



   func insertRowsMode3() {

        rows = 0

        insertRowMode3(ind: 0)
    }




  func insertRowMode3(ind:Int) {
        let indPath = IndexPath(row: ind, section: 0)
        rows = ind + 1
        tableView.insertRows(at: [indPath], with: .right)


        guard ind < place.count-1 else { return }
        DispatchQueue.main.asyncAfter(deadline: .now()+0.15) {

            self.insertRowMode3(ind: ind+1)
        }
    }



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

        return 1
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return rows
    }



    public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell

        cell.myImage.image = UIImage(named: (place[indexPath.row] + ".png"))
        cell.myLabel.text = place[indexPath.row]

        return (cell)
    }





    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

       performSegue(withIdentifier: "goToLast" , sender: place[indexPath.row])

    }


    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }








    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let location = locations[0]
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)

        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)

        self.map.showsUserLocation = true


  }





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

        let guest = segue.destination as! FinalClass

        guest.local = sender as! String




    }







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


}

and this is the code of the UIViewController: 这是UIViewController的代码:

import UIKit

class FinalClass: UIViewController {


    @IBOutlet weak var lastLabel: UILabel!

    @IBOutlet weak var addressLabel: UILabel!

    @IBOutlet weak var typeLabel: UILabel!

    @IBOutlet weak var lastImage: UIImageView!


    var local = "Zone"
    var localImage = UIImage()

    override func viewDidLoad() {
        super.viewDidLoad()

       lastLabel.text = local
       addressLabel.text = local
       typeLabel.text = local

        lastImage.image = localImage


    }

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

    @IBAction func lastBack(_ sender: Any) {

    dismiss(animated: true, completion: nil)
    }

}

for now i can only pass the "name" but not the rest, can someone help me with some example of code? 现在,我只能传递“名称”,而不能传递其余的名称,有人可以通过一些示例代码帮助我吗?

The proper way to do this is to pass the information via the prepare for segue function. 正确的方法是通过“准备segue”功能传递信息。 You can define properties in your FinalClass to pass your references to 您可以在FinalClass中定义属性,以将引用传递给

In your tableviewController 在你的tableviewController中

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToLast" {
       guard let vc = segue.destination as? FinalClass else { return }
       // set properties in your destination VC
       // Example
       // vc.photoProperty = photoFromTheTableViewController
    } 
}

In prepare(for segue:) , set the target view controller's localImage property. prepare(for segue:) ,设置目标视图控制器的localImage属性。

Mirroring the code you use to populate the cell, it would be: 镜像用于填充单元格的代码,它将是:

guest.localImage = UIImage(named: (sender as! String) + ".png")

But I agree with vadian's comment: I think you'll be happier, in the long run, with a Place construct that encapsulates the necessary data. 但是我同意vadian的评论:从长远来看,我认为使用封装必要数据的Place构造会更快乐。 Using the place name as a key feels brittle to me. 用地名作为键对我来说感觉很脆弱。

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

    let guest = segue.destination as! FinalClass

    guest.local = sender as! String
    guest.localImage = UIImage(named: (sender + ".png"))

}

For the address you can call Google place API from the user current location and I don't know about phone and website. 对于该地址,您可以从用户当前位置调用Google Place API,而我不知道电话和网站。

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

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