I have a table view cell which should get JSON data from an API and display it in the cell but it's empty. If I declare the same data locally and use it, it displays correctly.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
//createArray()
getBinData(userCompletionHandler: { data in
if let data = data {
self.createArray()
}
})
tableView.delegate = self
tableView.dataSource = self
self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
// Do any additional setup after loading the view.
}
Here is getBinData that basically gets the data from some API:
func getBinData(userCompletionHandler: @escaping (JsonObjectiveListClass?) -> Void) {
let session = URLSession.shared
let url = URL(string: "https://api.jsonbin.io/b/randombin/1")
let task = session.dataTask(with: url!, completionHandler: { (data, response, error) in
guard let data = data else {return}
do {
let welcome = try? JSONDecoder().decode(JsonObjectiveListClass.self, from: data)
userCompletionHandler(welcome)
}
})
task.resume()
}
func createArray() {
var tempObjectives: [Objective] = []
let image = getImageFromUrl(imageurl: "https://www.image.com")
let objective1 = Objective(image: image, title: "falcao TOTSSF", objectives: ["hello", "hello2"])
tempObjectives.append(objective1)
let objective2 = Objective(image: #imageLiteral(resourceName: "sabitzer"), title: "Sabitzer TOTSSF",objectives: ["hello", "hello2"])
tempObjectives.append(objective2)
objectives = tempObjectives
}