4

I am building a single viewcontroller app using UITableViewController in Swift 2.

The problem I am having here is if I load data inside viewDidLoad function, the UITableView displays well with the data.

However, I am trying to have get this data from a custom delegate method, which is called after the data is saved into core data, then self.tableview.reloadData() the UITableView won't display the data.

From my debugging, looks like the override func numberOfSectionsInTableView(tableView: UITableView) -> Int and override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

run before my custom delegate method

func didReceiveQualifications(qualifications: [Qualification]) {   
    print("didReceivedQualifications")
    self.qualifications = qualifications

    // Pass qualification to view
    self.tableView.reloadData()

}
2
  • That code and setup should actually work... Have you added print-statements to the two methods cellForRowAtIndexPath and numberOfSectionsInTableView as well to see at what time they get called? It is perfectly normal for them to get called before your delegate methods comes up. But they should be called once again after you do self.tableView.reloadData(). Additionally you might want to show the contents of the numberOfSectionsInTableView function. Commented May 2, 2016 at 10:19
  • I know it doesn't give you a proper answer but have you considered using NSFetchedResultsController? It makes the bridge between Core data objects and a UITableViewController for you. See developer.apple.com/library/mac/documentation/Cocoa/Conceptual/… Commented May 2, 2016 at 10:27

1 Answer 1

1

It can be depend by where didReceiveQualifications() has launched..in which thread running..

All updates to the UI should be done in the main thread.

Try to make reloadData to the main thread:

func didReceiveQualifications(qualifications: [Qualification]) {   
    print("didReceivedQualifications")
    self.qualifications = qualifications

    // Pass qualification to view
    dispatch_async(dispatch_get_main_queue()) {
        self.tableView.reloadData()
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@AlessandroOrnano, this works, however, if I start scrolling the UITableView, all the data will disappear
Have you any part in your code where you override the scrollViewDidScroll method, check it. Probably your code launch didReceiveQualifications at the end of the scroll to update datas, right?
@AlessandroOrnano, I actually found out that self.qualifications[indexPath.row].name starts return nil when I start scroll I didn't override scrollViewDidScroll method here is my code github.com/ankermarco/gojimo/blob/master/Gojimo/…
Apart from adding this code 'self.tableView.rowHeight = UITableViewAutomaticDimension self.tableView.estimatedRowHeight = 45.0' at the end of 'viewDidLoad' i cannot see any problems, have you check constraints of your cell?, try to set a prefixed constraint cell HEIGHT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.