我有要在 tableview 中显示的数据列表。API 适用于 POST 方法。但是我没有在 tableview 上获取数据,但它会打印在控制台上。我们不能从 Swift 中的 POST 方法中获取数据吗?请帮我解决这个问题。
import UIKit
import Alamofire
class SpendingsController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblHome: UITableView!
var dictData:NSArray = NSArray()
var appDictionary:NSDictionary!
var appDictionary2:NSDictionary!
override func viewDidLoad() {
super.viewDidLoad()
tblHome.delegate = self
tblHome.dataSource = self
self.postData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dictData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell3", for: indexPath) as! SpendingsCell
self.appDictionary = self.dictData.object(at: indexPath.row) as! NSDictionary
cell.spend_receipt?.text = self.appDictionary.value(forKey: "receipt_number") as? String
cell.store_amount?.text = self.appDictionary.value(forKey: "amount") as? String
cell.spend_storename?.text = self.appDictionary.value(forKey: "store_name") as? String
return cell
//Data are not fetched here.
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 156 //height for cell
}
func postData() {
let headers = [
"Authorization": "pZGFzbG9naW46QGRpZGFzJHRvcmU=" // its fake for privacy
]
let todosEndpoint = NSURL(string: "http://purchase_entries/Api/purchase_entry_list/")! as URL
let USersIDs = UserDefaults.standard.object(forKey: "UsersId")
let newTodo = ["user_id": USersIDs!]
print(newTodo)
Alamofire.request(todosEndpoint, method: .post, parameters: newTodo, encoding: URLEncoding.default, headers: headers)
.responseJSON { response in
debugPrint(response.result)
if let JSON = response.result.value{
print("ion json",JSON) // It prints the data
self.dictData = (JSON as AnyObject)["rows"] as! NSArray
}
}
}
}
在您的代码中进行更改:
Alamofire.request(todosEndpoint, method: .post, parameters: newTodo, encoding: URLEncoding.default, headers: headers)
.responseJSON { 响应在 debugPrint(response.result)
if let JSON = response.result.value{
print("ion json",JSON) // It prints the data
self.dictData = (JSON as AnyObject)["rows"] as! NSArray
if self.dictData.count > 0{
DispatchQueue.main.async {
yourTableView.reloadData()
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句