表视图数据未在 Alamofire 发布请求调用 swift 3 中显示

Posted

技术标签:

【中文标题】表视图数据未在 Alamofire 发布请求调用 swift 3 中显示【英文标题】:Tableview data not displaying in Alamofire post request call swift 3 【发布时间】:2017-04-28 11:01:27 【问题描述】:

我正在使用 API 来获取地址等用户信息。在这个应用程序中,用户可以输入多个地址,并且我按下了一个按钮,它将使地址成为 tableview 单元格中的默认地址。当我按下按钮将地址设为默认值时,它会将地址设为默认值,但不会在表格视图单元格中显示地址。甚至我也使用过 dispatch.main.async()。

我的按钮代码来设置默认地址。只是表格单元格在默认完成时不显示数据。

func makeAddressDefault(_ sender: UIButton) 
        let address = addresses[sender.tag]
        let params:Parameters = ["userid": Globals.sharedInstance.user!.id, "addressid": address.id!]
        let url = "/makeaddressdefault?userid=" + Globals.sharedInstance.user!.id
        RestApiManager.sharedInstance.makePostRequest(vc:self, url: url, post:params, successCompletionHandler:
            (data) in
            self.addresses.removeAll() // array of addresses
            self.reloadAddresses() // in this reload method in last i have added self.tableview.reload()
        , failureCompletionHandler:(data) in )
    

//这里定义了ReloadAddress方法

 private func reloadAddresses()
    
        Globals.sharedInstance.user!.addresses.removeAll()
        Globals.sharedInstance.getUserAddresses(vc: self)
        self.addresses = Globals.sharedInstance.user!.addresses

        self.tableView.reloadData()
    

//getUserAddresses()方法写在这里。

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
            
        
        
    


任何帮助将不胜感激。

【问题讨论】:

您对data 什么都不做?您只需删除地址中的所有内容并重新加载,没有任何内容触及您获得的新 data private func reloadAddresses() Globals.sharedInstance.user!.addresses.removeAll() Globals.sharedInstance.getUserAddresses(vc: self) self.addresses = Globals.sharedInstance.user!.addresses self. tableView.reloadData() 如果你能读懂这段代码。我只是删除数据并再次从 api 获取然后显示它。 当我移动到另一个屏幕并返回该屏幕时,它会显示数据但保持在同一屏幕上并使默认地址表显示空单元格。 更新你问题中的代码,顺便说一句,你应该在完成块内更新你的addresses ,用你得到的新data 我也在完成块中做了更新。 【参考方案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 )

【讨论】:

数据已在 getUserAddress() 方法中分配。感谢您的回复。 但您已将其删除 Globals.sharedInstance.user!.addresses.removeAll() 你能不能getUserAddresses这个方法,以便我们给出适当的答案。 在删除行之后,我编写了一个 getUserAddesses 方法,在该方法中我再次获取所有地址的朋友。 是通过异步调用获取数据吗?【参考方案2】:

您需要在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()
          
       

【讨论】:

以上是关于表视图数据未在 Alamofire 发布请求调用 swift 3 中显示的主要内容,如果未能解决你的问题,请参考以下文章

使用 RxSwift 将 Alamofire 请求绑定到表视图

对表格视图的每个单元格的 Alamofire 请求

使用 alamofire 和 swiftyjson 在 mysql 的表视图中显示数据

prepareForSegue 未在嵌入式 segue 中调用

JSON数据未在swift3中使用Alamofire进行解析

请求 Alamofire