2016-11-05 8 views
-2

私は、ユーザーがUITableから選択するインスタンスを持っています。選択したレコードにはnameとそれに関連付けられたidがあります。名前とIDを検証するための瞬間UITable DidSelectAtRowから変数を抽出するにはどうすればよいですか?

は正しく、私は

let tempCountryId = (self.newCountries[cellCountryId!]) 
    print (tempCountryId) 

国を使用していますが報告されています(名前:オプション( "イングランド")、countryId:オプション( "5"))

は、私は、データ(サッカー部門)と私のuitableを再増殖することができますので、私は番目やるん

どうcountryId「5」に一致する変数にcountryIdことを格納できるようにしたいですありますか?

これは私の完全なスクリプトです:

import UIKit 

class PickTeamViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet weak var teamsTableView: UITableView! 

var pickedCountryID: Int? 

var selectedCellCountryTitle: String? 
var cellCountryId: Int? 

struct Country { 
    var name: String? 
    var countryId: String? 

    init(_ dictionary: [String : String]) { 
     self.name = dictionary["name"] 
     self.countryId = dictionary["id"] 
    } 
} 


struct Divisions { 
    var divisionName: String? 
    var divisionId: String? 
    init(_ dictionary: [String : String]) { 

     self.divisionName = dictionary["name"] 
     self.divisionId = dictionary["country_id"] 
    } 
} 

struct Teams { 
    var teamName: String? 
    var newTeamId: String? 

    init(_ dictionary: [String : String]) { 
     self.teamName = dictionary["name"] 

    } 
} 

struct TeamId { 

    var newTeamId: String? 

    init(_ dictionary: [String : String]) { 
     self.newTeamId = dictionary["id"] 

    } 
} 

var newCountries = [Country]() 
var newDivisions = [Divisions]() 
var newTeams = [Teams]() 
var newTeamId = [TeamId]() 

override func viewDidAppear(_ animated: Bool) { 


    let myUrl = URL(string: "http://www.quasisquest.uk/KeepScore/getTeams.php?"); 
    var request = URLRequest(url:myUrl!); 
    request.httpMethod = "GET"; 

    let task = URLSession.shared.dataTask(with: myUrl!) { (data: Data?, response: URLResponse?, error: Error?) in 

     DispatchQueue.main.async 
      { 

       if error != nil { 
        print("error=\(error)") 
        return 
       } 

       do{ 
        let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any] 
        print (json) 

        if let arr = json?["countries"] as? [[String:String]] { 
         self.newCountries = arr.flatMap { Country($0) } 
         self.teamsTableView.reloadData() 

        } 


        if let arr = json?["divisions"] as? [[String:String]] { 
         self.newDivisions = arr.flatMap { Divisions($0) } 

        } 




        if let arr = json?["teams"] as? [[String:String]] { 
         self.newTeams = arr.flatMap { Teams($0) } 

        } 


        self.teamsTableView.reloadData() 
       } catch{ 
        print(error) 
       } 
     } 
    } 
    task.resume() 


} 


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

    return self.newCountries.count 
} 

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

    let country = newCountries[indexPath.row] 
    let cell = UITableViewCell() 

    cell.textLabel?.text = country.name 
    cell.textLabel?.font = UIFont(name: "Avenir", size: 12) 
    cell.textLabel?.textColor = UIColor.black 
    cell.backgroundColor = UIColor.white 

    return cell 

} 

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

    cellCountryId = indexPath.row 
    // print (self.newCountries[cellCountryId!]) 
    let tempCountryId = (self.newCountries[cellCountryId!]) 
    print (tempCountryId) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.teamsTableView.delegate = self 
    self.teamsTableView.dataSource = self 

    // Do any additional setup after loading the view. 
} 


} 
+0

選択したレコードがすでに選択されたレコードにある場合は、「TableViewCell」と表示します。表示する場所はどこですか? – Adeel

+0

あなたは何を意味するのか分かりません。しかし理想的には、レコードが選択されると、そのcountryId '5'を使用して、私が保存している別の配列のデータでUITableを更新します。 – RDowns

+0

同じ国で選択された国の詳細を表示しますか? 'TableView'? – Adeel

答えて

0

コメントで説明したように、詳細を表示するには別のビューコントローラを使用する必要があります。 didSelectRowAtIndexPathメソッドでは、newCountries配列から選択した国を取り出し、DetailViewControllerに渡します。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let countryDetailsVC = self.storyboard?.instantiateViewController(withIdentifier: "CountryDetailsViewController") as! DetailViewController 
    countryDetailsVC.country = selectedCountry 
    present(countryDetailsVC, animated: true, completion: nil) 
} 

は、今あなたがDetailViewControllerでその詳細を見ることができますStruct国を持っていること。

0

あなたのテーブルには、アレイnewCountriesから移入されます。したがって、テーブルの内容を置き換えるには、newCountriesの内容を置き換えて、テーブルをリロードする必要があります。

しかし、それは非常に賢明な戦略ではありません。異なるテーブルと異なるデータ配列を持つ異なるビューコントローラを表示する方が良いでしょう。

+0

私はこれをやってみるつもりです、ありがとう。 – RDowns

+0

私はまだ変数を必要としていますが、私は最初にそれを次のビューコントローラに渡すことをエールにすることを求めていました... – RDowns

関連する問題