我使用API来获取地址这样的用户信息。在这个应用程序中,用户可以输入多个地址,我按下了一个按钮,它将使地址成为tableview单元格中的默认地址。当我按下按钮以解决默认设置时,它会使地址成为默认值,但不显示tableview单元格中的地址。即使我使用了dispatch.main.async()。
我的按钮代码以制作默认地址。只有表格单元格在默认情况下才显示数据。
private func reloadAddresses()
{
Globals.sharedInstance.user!.addresses.removeAll()
Globals.sharedInstance.getUserAddresses(vc: self)
self.addresses = Globals.sharedInstance.user!.addresses
self.tableView.reloadData()
}
//此处定义了ReloadAddress mehtod。
public func getUserAddresses(vc:BaseViewController) {
if(Globals.sharedInstance.user!.addresses.count == 0)
{
let url = "/useraddresses?userid=" + Globals.sharedInstance.user!.id
RestApiManager.sharedInstance.makeGetRequest(vc:nil, url: url){
(data) in
// parse the result as JSON, since that's what the API provides
do {
guard let jsonResponse = try JSONSerialization.jsonObject(with: data, options: [])
as? [[String: Any?]] else {
print("error trying to convert data to JSON")
return
}
let foodieprofile = jsonResponse[0]
let addresses = foodieprofile["foodieprofile"] as! [String: Any?]
let jsonAddresses = addresses["deliveryaddress"] as! [[String: Any?]]
if(jsonAddresses.count>0 && Globals.sharedInstance.user!.addresses.count==0)
{
for index in 0...jsonAddresses.count-1 {
let this_address = jsonAddresses[index]
var isdeff = false
if let isdef = this_address["isdefault"] as? Bool
{
isdeff = isdef
}
let address = Address(id:this_address["_id"] as! String?, name:this_address["name"] as! String?, address:this_address["address"] as! String?, address2:this_address["address2"] as! String?, zone:this_address["zone"] as! String?, city:this_address["city"] as! String?, landmark:this_address["landmark"] as! String?, phone:this_address["phone"] as! String?, isdefault:isdeff)
Globals.sharedInstance.user!.addresses += [address]
}
}
} catch {
print("error trying to convert data to JSON")
return
}
}
}
}
}
//这里写的getUserAddresses()方法。
{{1}}
任何帮助将不胜感激。
答案 0 :(得分:1)
您需要在块内重新加载数据
RestApiManager.sharedInstance.makePostRequest(vc:self, url: url, post:params, successCompletionHandler:{
(data) in
self.addresses.removeAll() // array of addresses
Globals.sharedInstance.user!.addresses.removeAll()
Globals.sharedInstance.getUserAddresses = data // you need to assign your data it is for suggestion only
Globals.sharedInstance.getUserAddresses(vc: self)
self.addresses = Globals.sharedInstance.user!.addresses
self.tableView.reloadData()
}, failureCompletionHandler:{(data) in })
答案 1 :(得分:0)
您需要在getUserAddresses
方法中添加闭包,如下所示
public func getUserAddresses(vc:BaseViewController, callback: (_ issuccess: Bool) -> Void) {
if(Globals.sharedInstance.user!.addresses.count == 0)
{
let url = "/useraddresses?userid=" + Globals.sharedInstance.user!.id
RestApiManager.sharedInstance.makeGetRequest(vc:nil, url: url){
(data) in
// parse the result as JSON, since that's what the API provides
do {
guard let jsonResponse = try JSONSerialization.jsonObject(with: data, options: [])
as? [[String: Any?]] else {
print("error trying to convert data to JSON")
return
}
let foodieprofile = jsonResponse[0]
let addresses = foodieprofile["foodieprofile"] as! [String: Any?]
let jsonAddresses = addresses["deliveryaddress"] as! [[String: Any?]]
if(jsonAddresses.count>0 && Globals.sharedInstance.user!.addresses.count==0)
{
for index in 0...jsonAddresses.count-1 {
let this_address = jsonAddresses[index]
var isdeff = false
if let isdef = this_address["isdefault"] as? Bool
{
isdeff = isdef
}
let address = Address(id:this_address["_id"] as! String?, name:this_address["name"] as! String?, address:this_address["address"] as! String?, address2:this_address["address2"] as! String?, zone:this_address["zone"] as! String?, city:this_address["city"] as! String?, landmark:this_address["landmark"] as! String?, phone:this_address["phone"] as! String?, isdefault:isdeff)
Globals.sharedInstance.user!.addresses += [address]
callback(true) // return true.....add this line
}
}
} catch {
print("error trying to convert data to JSON")
callback(false) // return false.....add this line
return
}
}
}
}
}
通话方法
Globals.sharedInstance.getUserAddresses{ (issuccess) in
// check if success then reload table
if issuccess{
Globals.sharedInstance.user!.addresses.removeAll()
self.addresses = Globals.sharedInstance.user!.addresses
self.tableView.reloadData()
}
}