2016-03-19 11 views
-1

swftyjsonを使用してURLを取得しているjsonデータをテーブルビューに取り込もうとしています。この問題は、配列の都市を理解していますが、データをテーブルビューで使用すると問題になります。表示しないでください。どうすれば修正できますか?(私のjsonのデータ構造はURLをクリックして確認できます)。Swifyjson with tableview

import UIKit 
    import SwiftyJSON 
    import Alamofire 



    class ViewController: UIViewController , UITableViewDelegate ,UITableViewDataSource { 
@IBOutlet var tableview: UITableView! 

var data = [String]() 
var numberofRows = 0 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    Alamofire.request(.GET, "http://android.goidx.com/search") 
     .responseJSON {(response) -> Void in 


      if let value = response.result.value { 

       let json = JSON(value) 


       for (index,json):(String, JSON) in json { 
        //Do something you want 
        self.numberofRows = json["city"].count 


        let city = json["city"].string! as String 

        self.data.append(city) 
        print(json["city"].stringValue) 
       } 

    } 
    } 

    } 

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



func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 


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

    return numberofRows 
} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell" , forIndexPath: indexPath) as UITableViewCell 

    if data.count != 0 { 
    cell.textLabel?.text = data[indexPath.row] 

    } 

    return cell 
    } 




    } 

答えて

1

あなたは都市のあなたの配列を作成した後、あなたは内容

tableView.reloadData() 

をリフレッシュするために、テーブルビューを再ロードする必要があります。また、あなたのデータソースを変更、numberOfRowsInSection方法は

self.data.count 

を返すためにI返されたjsonのキーが配列にマップされないことに気付きました。

+0

いいえ動作していません –

+0

jsonを見ると、都市は配列ではないので、数は正しくありません。 numberOfRowsInSectionを変更して、代わりにself.data.countを返します。 –

+0

はい!私はそれをself.data.countに変更しましたが、テーブルビューには表示されません。これは、データ配列の出力がどのように出力され、複数の配列を与えているかを示しています。["Other County - Not In Usa"] ["Other County - Not In Usa"、 "Homestead"] ["その他の郡 - 米国ではない"、 "Homestead"、 "Doral"] ["その他の郡 - Not In Usa"、 "Homestead"、 "Doral "、" Hallandale "] –