3lvis / DATASource

Core Data's NSFetchedResultsController wrapper for UITableView and UICollectionView
Other
106 stars 27 forks source link

Number of sections returns one, even if theres is no objects #68

Closed amanbolat closed 8 years ago

amanbolat commented 8 years ago

I want show custom message when table view is empty, but it always return 1, even if theres is no data.

public func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        print(self.fetchedResultsController.sections?.count)
        return self.fetchedResultsController.sections?.count ?? 0
    }

But, when i use this function without implementing DATASource, just in my own table, it returns correct count of sections:

public func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        print(self.fetchedResultsController.sections?.count)
        return self.fetchedResultsController.sections?.count ?? 0
    }

Can't find any explanation for that.

3lvis commented 8 years ago

Overwriting any UITableViewDataSource breaks the functionality of DATASource, for your use case I would advise you to do something like:

// In your DATASource declaration
self.source = DATASource(...)
self.source.delegate = self
extension MyController: DATASourceDelegate {
    func dataSourceDidChangeContent(dataSource: DATASource) {
        if dataSource.isEmpty {
            // show custom message
        }
    }
}

I hope this helps, let me know if this doesn't work and I'll reopen the issue.

amanbolat commented 8 years ago

@3lvis Yes, you are right. I'm already using isEmpty variable to show or not show Empty message. However i don't understand why numberOfSectionsInTableView returns 1?

3lvis commented 8 years ago

@amanbolat I guess that's on your side, please make sure to not overwrite any UITableViewDataSource method on your controllers if you're using DATASource, otherwise you'll have unexpected behaviour.