【问题标题】:JSON displayed to tableviewJSON 显示到 tableview
【发布时间】:2016-11-27 18:54:03
【问题描述】:

我想第一次使用 AlamofireObjectMapper 来快速解析一个 json 响应。 我是这样映射的:

class ModelCurrency:  Mappable {

 var success   : Bool?
    var terms     : String?
    var privacy   : String?
    var timestamp : CGFloat?
    var source    : String?
    var quotes    : [Quotes]?

    init() {}

    required init?(map: Map) {

    }

    func mapping(map: Map) {

         success<-map["success"]
         terms<-map["terms"]
         privacy<-map["privacy"]
         timestamp<-map["timestamp"]
         source<-map["source"]
         quotes<-map["quotes"]

        print("It json\(terms)")
    }
}

class Quotes : Mappable {

    var name : String?
    var val : CGFloat?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        name<-map["name"]
        val<-map["val"]
    }
}

和我的控制器

 var arrayTable = [String]()

   override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        super.viewDidLoad()
        let URL = "http://www.apilayer.net/api/live?access_key=ad847a0a855c0647590df2b818923025"

        Alamofire.request(URL).responseObject { (response: DataResponse<ModelCurrency>) in

            let currency = response.result.value!;

            for quotes in currency.quotes! {


                self.arrayTable.append(quotes.name!)

            }
        }

    }


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

        return  5}

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

        return arrayTable.count   }


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


        cell.textLabel?.text = self.arrayTable[indexPath.row]


        return cell
        }

我希望在 tableview 中显示数组引号。怎么做 ?

【问题讨论】:

    标签: json swift uitableview objectmapper


    【解决方案1】:

    我认为你更新数据源arrayTable后需要reload表视图:

    ...
    for quotes in currency.quotes! {
      self.arrayTable.append(quotes.name!)
    }
    self.tableView.reloadData()
    ...
    

    此外,您无需拨打两次super.viewDidLoad()

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 2012-11-06
      • 1970-01-01
      • 2016-05-03
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      相关资源
      最近更新 更多