使用 Alamofire + SwiftyJSON 无法将数据加载到 UITableView
Data not load to UITableView with Alamofire + SwiftyJSON
在我的 iOS 应用程序中,我使用 Alamofire 进行休息请求,使用 SwiftyJSON 进行解析。其余 url 正在工作,我从服务器获取所有数据,我在控制台中打印它。但我无法使用 UITableView 填充该数据。我没有收到任何错误,项目编译和运行没有任何问题,但 table 视图是空的。这是我的简单代码:
var contracts = [Contract]()
override func viewDidLoad() {
super.viewDidLoad()
getContracts(contractSearchCriteria: ContractSearchCriteria())
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (contracts.count != 0) {
return contracts.count
}
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "contractCell", for: indexPath)
let contract = self.contracts[indexPath.row]
cell.textLabel?.text = contract.insuredPersonFullName
cell.detailTextLabel?.text = contract.companyName
return cell
}
这是 getContracts 方法:
func getContracts(contractSearchCriteria : ContractSearchCriteria) {
let params : Parameters = ["insuredPersonName": contractSearchCriteria.insuredPersonName]
Alamofire.request("\(Constants.restURL)getContracts", method: .post, parameters: params, encoding: JSONEncoding.default).validate().responseJSON(completionHandler: {response in
switch (response.result) {
case .success(let value):
let json = JSON(value)
let result = Result(json: json)
print(result.isSuccess)
for data in json["data"].arrayValue {
print(data)
self.contracts.append(Contract(json: data))
}
self.tableView.reloadData()
case .failure(let error):
print(error)
}
})
}
我使用 swift 3,在 Xcode 8 中使用带有 SwiftyJSON 的最新 Alamofire。我的代码有什么问题?我找不到任何解决方案。
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
在我的 iOS 应用程序中,我使用 Alamofire 进行休息请求,使用 SwiftyJSON 进行解析。其余 url 正在工作,我从服务器获取所有数据,我在控制台中打印它。但我无法使用 UITableView 填充该数据。我没有收到任何错误,项目编译和运行没有任何问题,但 table 视图是空的。这是我的简单代码:
var contracts = [Contract]()
override func viewDidLoad() {
super.viewDidLoad()
getContracts(contractSearchCriteria: ContractSearchCriteria())
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (contracts.count != 0) {
return contracts.count
}
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "contractCell", for: indexPath)
let contract = self.contracts[indexPath.row]
cell.textLabel?.text = contract.insuredPersonFullName
cell.detailTextLabel?.text = contract.companyName
return cell
}
这是 getContracts 方法:
func getContracts(contractSearchCriteria : ContractSearchCriteria) {
let params : Parameters = ["insuredPersonName": contractSearchCriteria.insuredPersonName]
Alamofire.request("\(Constants.restURL)getContracts", method: .post, parameters: params, encoding: JSONEncoding.default).validate().responseJSON(completionHandler: {response in
switch (response.result) {
case .success(let value):
let json = JSON(value)
let result = Result(json: json)
print(result.isSuccess)
for data in json["data"].arrayValue {
print(data)
self.contracts.append(Contract(json: data))
}
self.tableView.reloadData()
case .failure(let error):
print(error)
}
})
}
我使用 swift 3,在 Xcode 8 中使用带有 SwiftyJSON 的最新 Alamofire。我的代码有什么问题?我找不到任何解决方案。
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}