2017-05-17 41 views
0

我試圖像你這些圖像將數據傳遞迅速的UIViewController 3

enter image description here

此看到通過,在每一個細胞到一個UIViewController的信息是的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) 
    } 

} 

現在我只能通過「名稱」,但沒有休息,有人可以幫我一些例子的代碼?

+1

您只有數據源中的名稱。要有更多的參數,你需要一個自定義的結構或類作爲模型。 – vadian

+0

謝謝@vadian – bero

回答

0

正確的做法是通過準備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 
    } 
} 
+0

海報已經在代碼中準備了(for segue :)。 –

+0

海報正在使用強制downcasts。 –

+0

...這是一個不好的做法。但這也不是海報問的問題。 –

0

prepare(for segue:),設置目標視圖控制器的localImage財產。

鏡像用於填充單元格中的代碼,這將是:

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

但我vadian的評論表示贊同:我想你會更快樂,從長遠來看,一個地方構造,封裝必要的數據。使用地名作爲鑰匙對我來說感覺很脆弱。

+0

工作感謝@Joshua Kaden – bero

+0

非常好!聽到那個消息很開心。 –

0
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")) 

} 

對於地址,您可以從用戶當前位置調用Google place API,我不知道關於電話和網站。

+0

你不應該使用!在莊園裏,你在這裏使用它。這是一個沮喪的力量,並有可能造成難題。 –