我想将数据解析为tableviewcontroller,但它没有显示任何内容
我想访问密钥"列表"
中的标题,img_url和price_formatted用户输入他正在搜索的城市名称和家庭类型,我使用userdefaults保存这些值并在PropertySearchViewController中接收它们。
这是我使用almofire显示这些值的代码:
我有PropertySearchViewController,我在其中显示其中的值,PropertyTableViewCell和公共模型属性
1 -
=SUMPRODUCT(--({2,0,0,5}>2))
// PropertySearchViewController的结尾
2 -
=COUNT(IF({2,0,0,5}>2,1,""))
3 -
class PropertySearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let URL_Get_Data = "https://api.nestoria.co.uk/api?"
@IBOutlet weak var tableViewProperty: UITableView!
var properties = [Property]()
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return properties.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PropertyTableViewCell
let property :Property
property = properties[indexPath.row]
cell.propertyTitle.text = property.title
cell.propertyPrice.text = property.price
if property.imageUrl != nil {
Alamofire.request(property.imageUrl!).responseImage { response in
debugPrint(response)
if let image = response.result.value {
cell.propertyImage.image = image
}
else{
print("no image")
}
}}
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
//fetching data from web api
//recieve data
let city :String = UserDefaults.standard.string(forKey: "city")!
let type :String = UserDefaults.standard.string(forKey: "typeP")!
print("search values are :",city,type)
let params: [String: String] = ["encoding": "json", "pretty": "1", "action": "search_listings", "country": "uk", "listing_type": type, "place_name": city]
//end
//
Alamofire.request(URL_Get_Data, method: .get, parameters: params, encoding: URLEncoding.default, headers: nil).validate(statusCode: 200..<600).responseJSON {
// Alamofire.request(URL_Get_Data).responseJSON {
response in
// response in
//getting json
if let json = response.result.value {
print(type(of: json))
//converting json to NSArray
let propertyArray = json as! NSDictionary
//traversing through all elements of the array
for i in 0..<propertyArray.count{
//adding hero values to the hero list
self.properties.append(Property(
title: (propertyArray[i] as AnyObject).value(forKey: "title") as? String,
price: (propertyArray[i] as AnyObject).value(forKey: "price_formatted") as? String,
imageUrl: (propertyArray[i] as AnyObject).value(forKey: "imageUrl") as? String
))
}
//displaying data in tableview
self.tableViewProperty.reloadData()
}
}
}}
提前致谢
答案 0 :(得分:1)
(1)安装pod SwiftyJSON
(2)PropertySearchViewController中的import SwiftyJSON
(3)在let reuseIdentifierResultTable = "cell"
let URL_Get_Data = "url"
(4)加入viewDidLoad()
tableViewProperty.register(PropertyTableViewCell.self, forCellReuseIdentifier: reuseIdentifierResultTable)
tableViewProperty.delegate = self
tableViewProperty.dataSource = self
(5)将.responseJSON{}
中的每一个替换为
response in
if let data = response.data {
let json = String(data: data, encoding: String.Encoding.utf8)
//print(json)
if let dataFromString = json?.data(using: .utf8, allowLossyConversion: false) {
let json2 = JSON(data: dataFromString)
print("Response: \(json2)")
print("json status code: \(json2["response"]["status_code"])")
if json2["response"]["status_code"] == "200" && !(json2.null != nil){
print("json request count: \(json2["request"].count)")
print("json response count: \(json2["response"].count)")
print("json response listings count: \(json2["response"]["listings"].count)")
for i in 0...json2["response"]["listings"].count-1 {
print("\(i). \(json2["response"]["listings"][i]["title"])")
self.properties.append(Property(
title: json2["response"]["listings"][i]["title"].rawString()!,
price: json2["response"]["listings"][i]["price_formatted"].rawString()!,
imageUrl: json2["response"]["listings"][i]["img_url"].rawString()!
))
}
}
}
self.tableViewProperty.reloadData()
}
(6)替换为public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if properties.count < 1 {
return 0
}
return properties.count
}
(7)替换
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PropertyTableViewCell
同
let cell = Bundle.main.loadNibNamed("PropertyTableViewCell", owner: self, options: nil)?.first as! PropertyTableViewCell
(8)替换
cell.propertyTitle.text = property.title!
cell.propertyPrice.text = property.price!