2016-03-19 35 views
-1

我試圖用json數據填充我的tableview,即時通過swiftyjson獲取url。im面臨的問題是,即時通訊理解數組中的城市,但是當我在表格中使用它查看數據時不顯示。我可以如何解決它?(你可以通過點擊url來檢查我的json的數據結構)。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 

我注意到返回的json中的城市鍵不映射到數組。

+0

不,它不工作 –

+0

從查看JSON,城市不是一個數組,因此數不會是正確的。將您的numberOfRowsInSection更改爲返回self.data.count。 –

+0

是的!我確實把它改成了self.data.count,但是它沒有顯示在表格視圖中。這就是當我打印它時數據數組的輸出,它給了我多個數組。[「Other County - Not In Usa」] [「其他縣 - 不在美國」,「家園」] [「其他縣 - 不在美國」,「家園」,「多拉」] [「其他縣 - 不在美國」,「宅基地」,「多拉「,」Hallandale「] –