我有一些要在表格视图中显示的数据列表。该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
}
}
}
}
答案 0 :(得分:1)
更改代码:
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()
}
}
}
答案 1 :(得分:0)
我想这就是您所需要的:
func callLoginWebService() {
let token = SharedManager.getAuthenticationToken()
let headers = [
"token": token
]
var params = [String:String]()
params = [
"email":self.emailTxtField.text!,
"password":self.passwordTxtField.text!]
Alamofire.request(LoginUrl, method:.put, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
print("Request \(String(describing: response.request))")
print("RESPONSE \(String(describing: response.result.value))")
print("RESPONSE \(response.result)")
print("RESPONSE \(response)")
}
}
将response.result.value保存到数组或字典中。景气