self.tableView.reloadData() not working in Swift

前端 未结 10 1018
离开以前
离开以前 2020-12-23 09:12

I\'m attempting to learn Swift & the basics of iOS dev at the same time, so bear with me. I\'ve got a TableViewController that is

相关标签:
10条回答
  • 2020-12-23 09:42

    You have just to enter:

    First a IBOutlet:

    @IBOutlet var appsTableView : UITableView
    

    Then in a Action func:

    self.appsTableView.reloadData()
    
    0 讨论(0)
  • 2020-12-23 09:42

    Beside the obvious reloadData from UI/Main Thread (whatever Apple calls it), in my case, I had forgotten to also update the SECTIONS info. Therefor it did not detect any new sections!

    0 讨论(0)
  • 2020-12-23 09:43

    So, the issue was that I was trying to inappropriately use @lazy, which caused my Business variable to essentially be a constant, and thusly uneditable. Also, instead of loading the local json, I'm now loading only the data returned from the API.

    import UIKit
    
    class BusinessTableViewController: UITableViewController {
    
        var data: NSMutableData = NSMutableData()
        var Business: NSMutableArray = NSMutableArray()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            navigationItem.titleView = UIImageView(image: UIImage(named: "growler"))
    
            tableView.registerClass(BeerTableViewCell.self, forCellReuseIdentifier: "cell")
            tableView.separatorStyle = .None
    
            fetchKimono()
        }
    
        override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
            return Business.count
        }
    
        override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
            if (Business.count > 0) {
                let biz = Business[section] as NSDictionary
                let beers = biz["results"] as NSArray
                return beers.count
            } else {
                return 0;
            }
        }
    
        override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
            let cell = tableView!.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath!) as BeerTableViewCell
            if let path = indexPath {
                let biz = Business[path.section] as NSDictionary
                let beers = biz["results"] as NSArray
                let beer = beers[path.row] as NSDictionary
    
                cell.titleLabel.text = beer["BeerName"] as String
            } else {
                cell.titleLabel.text = "Loading"
            }
    
            return cell
        }
    
        override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
            let view = LocationHeaderView()
            let biz = Business[section] as NSDictionary
            if (Business.count > 0) {
                let count = "\(Business.count)"
                view.titleLabel.text = (biz["name"] as String).uppercaseString
            }
            return view
        }
    
        override func tableView(tableView: UITableView!, heightForHeaderInSection section: Int) -> CGFloat {
            return 45
        }
    
        func fetchKimono() {
            var urlPath = "names have been removed to protect the innocent"
            var url: NSURL = NSURL(string: urlPath)
            var request: NSURLRequest = NSURLRequest(URL: url)
            var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
    
            connection.start()
        }
    
        func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
            // Recieved a new request, clear out the data object
            self.data = NSMutableData()
        }
    
        func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
            // Append the recieved chunk of data to our data object
            self.data.appendData(data)
        }
    
        func connectionDidFinishLoading(connection: NSURLConnection!) {
            // Request complete, self.data should now hold the resulting info
            // Convert the retrieved data in to an object through JSON deserialization
            var err: NSError
            var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
            var results: NSDictionary = jsonResult["results"] as NSDictionary
            var collection: NSArray = results["collection1"] as NSArray
            if jsonResult.count>0 && collection.count>0 {
                Business = jsonResult
                tableView.reloadData()
            }
        }
    }
    

    Swift Docs on @lazy:

    You must always declare a lazy property as a variable (with the var keyword), because its initial value may not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization completes, and therefore cannot be declared as lazy.

    0 讨论(0)
  • 2020-12-23 09:48

    You must reload your TableView in main thread only. Otherwise your app will be crashed or will be updated after some time. For every UI update it is recommended to use main thread.

    //To update UI only this below code is enough
    //If you want to do changes in UI use this
    DispatchQueue.main.async(execute: {
        //Update UI
        self.tableView.reloadData()//Your tableView here
    })
    
    //Perform some task and update UI immediately.
    DispatchQueue.global(qos: .userInitiated).async {  
        // Call your function here
        DispatchQueue.main.async {  
            // Update UI
            self.tableView.reloadData()  
        }
    }
    
    //To call or execute function after some time and update UI
    DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
        //Here call your function
        //If you want to do changes in UI use this
        DispatchQueue.main.async(execute: {
            //Update UI
            self.tableView.reloadData()
        })
    }
    
    0 讨论(0)
  • 2020-12-23 09:49

    In my case the table was updated correctly, but setNeedDisplay() was not called for the image so I mistakenly thought that the data was not reloaded.

    0 讨论(0)
  • 2020-12-23 09:51

    Try it: tableView.reloadSections(IndexSet(integersIn: 0...0), with: .automatic) It helped me

    0 讨论(0)
提交回复
热议问题