How to set numberOfRowsInSection from an alamofire request?

0

I need to take the value of my request to set the value of the rows

func getProjects(_ role:Int, _ idUser:Int, done:@escaping (_ response:JSON)-> Void){
        let parameters:Parameters = ["role":role,"idUser":idUser]


 Alamofire.request("http://localhost:8000/value",method:.post,parameters:parameters).responseJSON{
            response in
            let json = JSON(response.value)
            done(json)
        }

}

I take the value of the json to set it in the function and try with viewWillAppear but because it is asynchronous you can not

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return json.count
}
    
asked by David 25.05.2018 в 01:17
source

2 answers

0

first you must make a parsession of that json, you should include in your question the content of the json to be more in context .....

and in your viewcontroller where you call the getProjects function

        var rows = 0 

        getProjects(role:Int, _ idUser:Int){(response) in 
            self.rows = response
            self.tableview.reloadData()
        }

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return rows
     }
    
answered by 25.05.2018 в 17:55
0

Complementing the answer that is giving you sergio, since where you would get the value is to remove it in the CompletionHandler you have, and you set it to a universal variable var jsonValue = [String: Any] ()' , that you set when you get the value, of course the variable dependdara if you are receiving a JsonObject. I'll give you the following example.

var jsonValue = [String : Any]() //suponiendo que es un json object lo que vas a recibir {'key': 'value'}
//ahora cuando mandes a llamar tu método
getProjects(1, CompletionHandler: {(response)
   self.jsonValue = response as! [String : Any]//como explique en el caso que estes recibiendo un jsonObject
   //haces el relogueo de la table
   self.table.reload()//nombre de la variable de tu tableView
})

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return jsonValue.count
}
    
answered by 25.05.2018 в 18:13