Swifyjson与tableview

时间:2016-03-19 23:51:31

标签: ios json swift uitableview swifty-json

我正在尝试使用swiftyjson在我的URL上填充json数据填充我的tableview。我面临的问题是,我正在理解数组中的城市但是当我在表格视图上使用它时数据不会显示。我该如何解决?(你可以通过点击网址查看我的json的数据结构。)

  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 个答案:

答案 0 :(得分:1)

创建城市数组后,需要重新加载表格视图以刷新内容

SELECT SUM (
   SELECT n.hodnota 
   FROM evidn_nastaveni n
   WHERE n.popis = 'cena_krakorce'
   AND
   n.upresneni = k.pozice) 
   AS celkova_cena
FROM (
  SELECT oc.clen, oc.oddil
  FROM evidn_oddily_clenove oc 
  INNER JOIN evidn_oddily o ON oc.oddil = o.id
  WHERE o.nazev = 'Slalom')
NATURAL JOIN evidn_krakorce k;

同时更改数据源,numberOfRowsInSection方法返回

tableView.reloadData()

我注意到返回的json中的cities键没有映射到数组。