如何在Swift 1.2中显示json响应?

时间:2017-04-26 07:34:31

标签: swift

我已经在目标c的表格视图中显示了json响应,但我无法使用Swift。

  

我的JSON回应:“{”状态“:”成功“,”结果“:[{”id“:”10“,”名称“:”SRS TOWER“},{”id“:”9“, “name”:“123 Fake St - Investor 2 Eco”},{“id”:“8”,“name”:“VARDHMAN MALL”},{“id”:“6”,“name”:“GIP MALL施工 “}]}”

任何帮助都会很棒

1 个答案:

答案 0 :(得分:0)

这个方法都在swift 3.0声明数组

var allData = [AnyObject]()

从json数据中分配值

    let responsedata = try JSONSerialization.jsonObject(with: data! as Data, options:.allowFragments) as! NSDictionary

    allData = dictValue.value(forKey: "result") as! [AnyObject]
    self.tableview.reloadData()

所有表veiw数据源方法

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

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

    return allData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    for item in cell.contentView.subviews
    {
        if item.tag == -55
        {
            item.removeFromSuperview()
        }
    }

    let dictValue = allData[indexPath.row] as! NSDictionary
    let name = dictValue.value(forKey: "name") as! String

    let viewForAll = UIView()
    viewForAll.frame = CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 44)
    viewForAll.tag = -55
    cell.contentView.addSubview(viewForAll)

    let dateValue = UILabel()
    dateValue.frame = CGRect(x: 60, y: 0, width: tableView.frame.size.width-50, height: 44)
    dateValue.text = name
    dateValue.textAlignment = .left
    dateValue.font = UIFont.systemFont(ofSize: 15)
    dateValue.textColor = UIColor.black
    viewForAll.addSubview(dateValue)

    cell.selectionStyle = .none
    return cell
}