GMCoreDataMultipleSectionsTableVC

UITableViewController subclass backed by multiple NSFetchedResultsControllers


License
MIT
Install
pod try GMCoreDataMultipleSectionsTableVC

Documentation

GMCoreDataMultipleSectionsTableVC

UITableViewControllers are most of the time backed with only one NSFetchedResultsController.

This subclass allows you to easily handle multiple NSFetchedResultsControllers, each under a section with a custom title.

Drawing

How To Use It

class YourTableViewController: CoreDataMultipleSourcesTableVC {
    
    var managedObjectContext: NSManagedObjectContext? {
        didSet{
            self.sectionTitles = [<# YOUR SECTION TITLES #>]
            self.fetchedResultsControllers = [<# YOUR FETCHED RESULTS CONTROLLERS #>]
        }
    }
    
    func setupFetch() {
      self.managedObjectContext = <# YOUR MANAGED OBJECT CONTEXT #>
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupFetch()
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell: UITableViewCell?
        
        switch(indexPath.section){
        case 0:
            cell = createCellExampleOne(tableView, forOriginalIndexPath: indexPath)
        case 1:
            cell = createCellExampleTwo(tableView, forOriginalIndexPath: indexPath)
        default:
            break
        }
        
        if cell == nil {
            cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell?
            if cell == nil {
                cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
            }
        }
        
        return cell!
    }
    
    func createCellExampleOne(tableView: UITableView, forOriginalIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell: UITableViewCell
        let indexPathComputed = NSIndexPath(forRow: indexPath.row, inSection: 0)
        
        if let c = self.tableView.dequeueReusableCellWithIdentifier("cellExampleOne") as UITableViewCell? {
            cell = c as UITableViewCell
        }else{
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellExampleOne")
        }
        
        let managedObject = self.fetchedResultsControllers[indexPath.section].objectAtIndexPath(indexPathComputed) as NSManagedObject
        
        cell.textLabel?.text = managedObject.title
        
        return cell
    }
    
    func createCellExampleTwo(tableView: UITableView, forOriginalIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell: UITableViewCell
        let indexPathComputed = NSIndexPath(forRow: indexPath.row, inSection: 0)
        
        if let c = self.tableView.dequeueReusableCellWithIdentifier("cellExampleTwo") as UITableViewCell? {
            cell = c as UITableViewCell
        }else{
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellExampleTwo")
        }
        
        let managedObject = self.fetchedResultsControllers[indexPath.section].objectAtIndexPath(indexPathComputed) as NSManagedObject
        
        cell.textLabel?.text = managedObject.title
        
        return cell
    }    

}

Future

  • Allow Mix of NSFetchedResultController & Custom Static/Dynamic Arrays
  • Allow NSFetchedResultsController with multiple sections and merge all

Help?

@_imton | gaston@black.uy

Keywords

  • Multiple NSFechedResultsController in UITableViewController
  • Mix Static and Dynamic content on UITableViewController
  • Two NSFechedResultsController in UITableViewController