ios - 将数据从 tableView 传递到 UIViewController swift 3

标签 ios swift xcode

我正在尝试将每个单元格中的信息传递给 UIViewController,就像您在这些图像中看到的那样

enter image description here

这是 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.
    }


}

这是 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)
    }

}

目前我只能传递“名称”,而不能传递其余部分,有人可以帮我提供一些代码示例吗?

最佳答案

执行此操作的正确方法是通过prepare for segue 函数传递信息。您可以在 FinalClass 中定义属性以将引用传递给

在你的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
    } 
}

关于ios - 将数据从 tableView 传递到 UIViewController swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44030932/

相关文章:

ios - 如何以编程方式设置音频文件的特定顺序?

swift - 从 ReactiveCocoa 2.4.7 跳到 ReactiveCocoa 5.0.0

swift - 在 map 中打开 MapKit pin

ios - 无法更改其中的搜索栏和文本字段的高度

objective-c - 如何将char *转换为float xcode

ios - 将 iOS PT 转换为 Android DP?

ios - MKMapView addAnnotation 不起作用

ios - Swift UITextField 约束 - 在 View 中间添加文本字段

ios - 如何使用 "if"语句检测 iAd 是否已加载

objective-c - Objective-C 弧 : How to remove an object from an array/set and then return it as an autoreleased object?