2016-09-01 9 views
0

私は国のリストを持つテーブルビューであるメインビューを持っています。任意の国名(セル)をクリックすると、国の名前を次のView Controllerのナビゲーションバーのタイトルに渡すsegue経由で別のビューが読み込まれます。2度目の読み込みまでにビューが更新されない

問題は最初のクリックではタイトルが更新されないが、戻るボタン(現在のビューを閉じる)をクリックして別の国名をクリックすると、2番目のビューが再び読み込まれ、前のタイトルが表示される最初の試行で表示されます。

第メインビュー・コントローラのためのコード:

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    var sectionsArray = [String]() 
    var sectionsCountries = [Array<AnyObject>]() 

    @IBOutlet weak var countries: UITableView! 

    internal func numberOfSections(in tableView: UITableView) -> Int { 
     // Return the number of sections. 
     return self.sectionsArray.count 
    } 

    internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // Return the number of rows in the section. 
      return self.sectionsCountries[section].count 
    } 

    internal func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return self.sectionsArray[section] 
    } 

    internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CountryCell", for: indexPath) 
     cell.textLabel?.text = self.sectionsCountries[indexPath.section][indexPath.row] as? String 
     return cell 
    } 

    var valueToPass:String! 

    internal func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     print("You selected cell #\(indexPath.row)!") 

     // Get Cell Label 
     let indexPath = tableView.indexPathForSelectedRow; 
     let currentCell = tableView.cellForRow(at: indexPath!) as UITableViewCell!; 

     valueToPass = currentCell?.textLabel?.text 
     performSegue(withIdentifier: "cellSegue", sender: self) 
     //print(valueToPass) 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "cellSegue" { 
      let destination = segue.destination as! CountryViewController 
      destination.passedValue = valueToPass 
     } 
    } 

    override func viewDidLoad() { 

     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     let url = URL(string: "http://cyber7.co.il/swift/countries/countries-list.json")! 
     let task = URLSession.shared.dataTask(with: url) { (data, response, error) in 
      if error != nil { 
       print(error) 
      } else { 
       if let urlContent = data { 
        do { 
         let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) 
         for result in jsonResult as! [Dictionary<String, AnyObject>]{ 
          self.sectionsArray.append(result["sectionName"] as! String) 
          self.sectionsCountries.append(result["sectionCountries"] as! Array<String> as [AnyObject]) 
         } 
        } catch { 
         print("JSON Processing Failed") 
        } 
        DispatchQueue.main.async(execute: {() -> Void in 
         self.countries.reloadData() 
        }) 
       } 
      } 
     } 
     task.resume() 
    } 

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


} 

第2のビューコントローラのためのコード:

import UIKit 

class CountryViewController: UIViewController { 

    var passedValue:String! 

    @IBOutlet weak var navBar: UINavigationBar! 

    @IBAction func backButton(_ sender: AnyObject) { 
     self.dismiss(animated: true, completion: nil) 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     self.navBar.topItem?.title = passedValue 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 
    } 

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

} 

答えて

0

ビューにテーブルビューセルから設定セグエを有しますコントローラはストーリーボード内にあり、セルが選択されると自動的に実行されます。あなたのセル選択方法でセグを実行するための呼び出しは、最初のセグが既に実行された後に2回目にセグを実行しています。

はあなたのtableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)方法を取り外し、prepareForSegueのロジックを通過するすべてのデータの操作を行います。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "cellSegue" { 
     let destination = segue.destination as! CountryViewController 
     let indexPath = countries.indexPathForSelectedRow 
     let currentCell = countries.cellForRow(at: indexPath!) 
     destination.passedValue = currentCell?.textLabel?.text 
    } 
} 
+0

「didSelectRowAt」を使用していない場合は、どのように私は「.textLabel currentCellの.text?」へのアクセスを得ることができますか? –

+0

私の答えに書いたコードは、あなたにそれを行う方法を示しています..? – dan

+0

あなたのお手伝いをしていただきありがとうございます。 –

関連する問題