2017-05-22 16 views
0

tableViewのどのセルがクリックされたかによって文字列を設定しようとしています。 BlueLineTableViewControllerは、ユーザーのクリックをキャプチャするものです。Swift 3 - クリックしたセルに応じてTableViewController内の変数を設定する

import UIKit 

class BlueLineTableViewController: UITableViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

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

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

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

    let station = bluelinestations[indexPath.row] 
    cell.textLabel?.text = station.name 
    cell.imageView?.image = UIImage(named: station.image) 
    return cell 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let row = indexPath.row 
    if row == 0 { 
     BlueBelmontTableViewController().feed = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40890&outputType=JSON" 
    } 
    if row == 1 { 
     BlueBelmontTableViewController().feed="http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40820&outputType=JSON" 
    } 
} 

BlueBelmontTableViewControllerのフィード変数は、/ BlueLineTableViewControllerにクリックされたセルに応じて、別のURLに設定して変更する必要があります。

import UIKit 

class BlueBelmontTableViewController: UITableViewController { 

class Destinations { 
    var destination: String = "" 
    var time: String = "" 
} 

var feed = "" 

var dataAvailable = false 

var records = [Destinations]() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    parseData() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

    for r in records { 
     r.time = "" 
     r.destination = "" 
    } 
} 

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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return dataAvailable ? records.count : 15 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if (dataAvailable) { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     let destinationRow = records[indexPath.row] 
     cell.textLabel?.text = destinationRow.destination 
     cell.detailTextLabel?.text = destinationRow.time 
     return cell 
    } else { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "PlaceholderCell", for: indexPath) 
     return cell 
    } 
} 

func parseData() { 
    guard let feedURL = URL(string: feed) else { 
     return 
    } 
    let request = URLRequest(url: feedURL) 
    let task = URLSession.shared.dataTask(with: request) {(data, response, error) in 
     if error != nil 
     { 
      print("Error") 
     } 
     else { 
      if let content = data { 
       do { 
        let json = try JSONSerialization.jsonObject(with: content, options: []) as? [String:Any] ?? [:] 
        print(json) 
        if let ctattimetable = json["ctatt"] as? [String:Any] { 
         if let estArrivalTime = ctattimetable["eta"] as? [[String:Any]] { 
          for item in estArrivalTime{ 
           if let headingTowards = item["destNm"] as? String, 
            let arrivalTime = item["arrT"] as? String { 
            let record = Destinations() 
            record.destination = headingTowards 
            record.time = arrivalTime 
            self.records.append(record) 
           } 
           self.dataAvailable = true 
           DispatchQueue.main.async { 
            self.tableView.reloadData() 
           } 

          } 
         } 
        } 
       } 
       catch { 
       } 
      } 
     } 
    } 
    task.resume() 
} 
} 

私はBlueLineTableViewControllerに見られるようにindexPath.rowに応じたdidSelectRowAt方法でURLを設定しようとしましたが、何もしていないようです。誰も私がこれをやっていく方法を知っていますか?

enter image description here

答えて

3

あなたは値を渡すことができません。

prepareForSegueを無効にして、BlueBelmontTableViewControllerに値を渡す必要があります。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    let vc = segue.destination as! BlueBelmontTableViewController 
    if let indexPath = self.tableView.indexPathForSelectedRow { 
     if indexPath.row == 0 { 
      vc.feed = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40890&outputType=JSON" 
     } 
     if indexPath.row == 1 { 
      vc.feed = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40820&outputType=JSON" 
     } 
    } 
} 
+0

ありがとうございます!私はindexPath = self.tableView.indexPathForSelectedRow?.rowが動作するようにしなければならなかったが、それ以外の場合は完全に動作する(問題があるかどうかわからないがswitch index {case 0:vc .feed = "url"など。 – moesyzlack23

+1

@ moesyzlack23ようこそメイト:)、私の編集した答えをチェックして、あなたの状態を 'indexPath.row'に変更して、あなたにすべて行くように設定しないでください。 –

+0

ありがとう、それは動作します!好奇心が強い、2つの実装の違いはありますか?それはどちらにしてもうまくいくようです。 – moesyzlack23

0

代わりの

BlueBelmontTableViewController().feed = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40890&outputType=JSON" 

使用

self.feed = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40890&outputType=JSON" 

BlueBelmontTableViewController beacause()新しい仮調印されています。以下は

は私のプロジェクトのこの部分のMain.storyboardですBlueBelmontTableViewControllerのインスタンスと既存のインスタンスを変更したいので、新しいインスタンスを作成する代わりにselfを使用する必要があります。あなたが BlueBelmontTableViewControllerの完全に新しいインスタンスではありません、あなたの UITableViewCellから BlueBelmontTableViewControllerに作成したあなたの segueを使用してナビゲーションスタックに追加されたものに feedプロパティを設定しているので、

関連する問題