Search code examples
uitableviewcore-datansfetchedresultscontrollerswift3

Swift 3. Hiding an empty tableView using Core Data


I have tried to hide my tableView when it's empty using following line of code inside of the viewDidAppear and viewDidLoad methods:

let count = self.fetchedResultsController.fetchedObjects?.count == 0
    self.tableView.isHidden = count

But it's doesn't work. How to hide a tableView when it's empty?


Solution

  • Do you really need to hide the table when it's empty? I would rather provide one big table row with information like 'No results found' or something.

    But if you need to display other UI stuff when no results:

    Try to use the function with fetchRequest.

    let count = managedContext.countForFetchRequest(fetchRequest, error: nil) == 0