I want to populate my Table View with a bunch of folders(similar to the folder structure screen of the Apple Notes App). I want the first item in the TV to be a cell that leads to a combination of all the contents from the other folders. I can display all the folders but am having trouble trying to work out how to display the 'combo contents' folder. All the individual folders are made on CoreData and are being fed through a NSFetchedResultsController.
I created an array of just a single folder instance to use as the object for the combo folder. I have the following code to try and create 2 sections (1 for the combo, the other for the individual folders) but at the moment it will only show whatever I put in case 0. Am I going about this the right way?
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return comboFolder.count
case 1:
return fetchedRC.fetchedObjects?.count ?? 0
default:
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "folderCell", for: indexPath) as! FolderTableViewCell
switch indexPath.section {
case 0:
let folder = comboFolder[indexPath.row]
cell.updateComboFolder(with: folder)
case 1:
let folder = fetchedRC.object(at: indexPath)
cell.update(with: folder)
default:
print("default")
}
return cell
}
Here's a sample project that has your basic setup - https://github.com/nambatee/CombinedFolders1
The trick is to adjust the indexPath
. You're working with 2 sections but your NSFetchedResultsController
knows of only 1 section. So you need to be careful not to ask NSFetchedResultsController
for objects in section at index 1
.