gpt4 book ai didi

ios - 使用 Alamofire + SwiftyJSON 无法将数据加载到 UITableView

转载 作者:行者123 更新时间:2023-11-29 00:36:38 25 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我使用 Alamofire 进行休息请求,使用 SwiftyJSON 进行解析。其余的 url 正在工作,我从服务器获取所有数据,我在控制台中打印它。但我无法使用 UITableView 填充该数据。我没有收到任何错误,项目编译和运行没有任何问题,但 TableView 是空的。这是我的简单代码:

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)
}


})

}

我在 Xcode 8 中使用 swift 3 和带有 SwiftyJSON 的最新 Alamofire。我的代码有什么问题?我找不到任何解决方案。

最佳答案

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

关于ios - 使用 Alamofire + SwiftyJSON 无法将数据加载到 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40450287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com