iOS: tableView.reloadData() doesn't work in swift

孤街浪徒 提交于 2019-11-30 19:31:26

In your refresh function, your load completes asynchronously using a closure, but you are updating your activity indicator and reloading your table outside of the closure, so it will execute before the load is completed. You need to move this code inside the closure and ensure that it executes on the main queue (as it updates the UI)

func refresh(sender: AnyObject) {
        // Afficher l'icône de chargement dans la barre de status
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true

        // On télécharge les autoroutes
        var url = NSURL(string: "http://theurl.com")! // URL du JSON
        var request = NSURLRequest(URL: url) // Création de la requête HTTP
        var queue = NSOperationQueue()  // Création de NSOperationQueue à laquelle le bloc du gestionnaire est distribué lorsque la demande complète ou échoué

        // Envoi de la requête asynchrone en utilisant NSURLConnection
        NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{(response: NSURLResponse!, data: NSData!, error: NSError!) ->Void in
            // Gestion des erreurs de connexion
            if error != nil {
                // Masquer l'icône de chargement dans la barre de status
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false

                println(error.localizedDescription)
                let errorAlert = UIAlertView(title: "Erreur", message: error.localizedDescription, delegate: self, cancelButtonTitle: "OK")
                errorAlert.show()
            }
            else {
                // Récupération du JSON et gestion des erreurs
                let json = JSON(data: data)

                if let highwaysData = json.arrayValue {
                    for highway in highwaysData {
                        var newHighway = Highway(ids: highway["Ids"].stringValue, name: highway["Name"].stringValue, label: highway["Direction"].stringValue, length: highway["Length"].stringValue, directions: highway["Direction"].stringValue, operateur: highway["Operator"].stringValue)
                        self.highways.append(newHighway)
                    }
                }
            }

            dispatch_async(dispatch_get_main_queue(), {

                if (self.refreshControl!.refreshing) {
                    self.refreshControl!.endRefreshing()
                }

                self.tableView.reloadData() 

                // Masquer l'icône de chargement dans la barre de status
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            })

        })

    }

If you are using a tab bar controller (which it sounds like you are), and are expecting a refresh on the table after modifying something in another tab, you will need to call tableView.reloadData() from inside of the destination view controller's viewWillAppear() or viewDidAppear() method instead. viewDidLoad() is only called once for the life of that view controller.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!