Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Genady Novak
Genady Novak
5,164 Points

Floating Header

Hi, I'm trying to create a feed that looks like Instagram. I'm using NSFetchedResultsController the save my data and when I take an image I want this image on my feed. I don't have an bug when I'm using the "regular" feed like in FaceBook (one section and lots of rows) but when I trying to implement one row per section to create a floating header like in Instagram feed the the new image doesn't show on the feed and the feed get messed up

this is the part of the code for inserting new data to my table view controller:

func controllerWillChangeContent(controller: NSFetchedResultsController) {
    self.tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {


    switch type{
    case NSFetchedResultsChangeType.Insert:
        self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Top)

        break
    case NSFetchedResultsChangeType.Delete:
        self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Left)
        break
    case NSFetchedResultsChangeType.Update:



        self.tableView.cellForRowAtIndexPath(indexPath!)?.setNeedsLayout()
        break
    default:
        return
    }
}

func controller(controller: NSFetchedResultsController!, didChangeSection sectionInfo: NSFetchedResultsSectionInfo!, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
    let indexSet = NSIndexSet(index: sectionIndex)
    switch type {
    case NSFetchedResultsChangeType.Insert:
        self.tableView.insertSections(indexSet, withRowAnimation: UITableViewRowAnimation.Fade)
    case NSFetchedResultsChangeType.Delete:
        self.tableView.deleteSections(indexSet, withRowAnimation: UITableViewRowAnimation.Fade)
    case NSFetchedResultsChangeType.Update:
        break
    case NSFetchedResultsChangeType.Move:
        break
    }
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
    self.tableView.endUpdates()
}