Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 28, 2012, 06:33 PM   #1
mdhansen5
macrumors member
 
Join Date: Nov 2010
Location: Colorado
Dividing UITableView into sections by Core Data Attribute

I'm quite the newbie at Core Data, so please bear with me.

In my Data Model, I have a single entity called "Item", with 2 attributes, "Section" and "Name." I already have the UITableView (my main view) populated using by Name, but I want to divide the TableView into 3 different sections, determined by attribute "section." I'm thinking that the "section" attribute should be an integer, so 0 would be the first section, 1 would be the second and 2 would be the third.

I really am not sure how to divide the TableView into the sections. I'm using NSFetchedResultsController. Here's the current code.

Code:
- (void)setupFetchedResultsController
{
    // 1 - Decide what Entity you want
    NSString *dbEntityName = @"Item"; // Put your entity name here
    
    // 2 - Request that Entity
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:dbEntityName];
    
    // 3 - Filter it if you want
    //request.predicate = [NSPredicate predicateWithFormat:@"Item.name = what"];
    
    // 4 - Sort it if you want
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name"
                                                                                     ascending:YES
                                                                                      selector:@selector(localizedCaseInsensitiveCompare:)]];
    // 5 - Fetch it
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.managedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];
    [self performFetch];
}
Also, I was wondering if the Fetch Request template inside the data model in Xcode would help out at all. I see how to create the fetch request, but not how to make it do anything with what it fetches (ex: put the fetched data into a certain TableView section.)

If anyone could help out, it would be greatly appreciated.
mdhansen5 is offline   0 Reply With Quote
Old May 29, 2012, 01:59 AM   #2
Scott90
macrumors 6502
 
Join Date: Jul 2008
The sectionNameKeyPath argument is what defines your sections. If you set that to the attribute that indicates what your sections are, the table view will automatically include sections.
Scott90 is offline   0 Reply With Quote
Old May 29, 2012, 10:09 AM   #3
mdhansen5
Thread Starter
macrumors member
 
Join Date: Nov 2010
Location: Colorado
Quote:
Originally Posted by Scott90 View Post
The sectionNameKeyPath argument is what defines your sections. If you set that to the attribute that indicates what your sections are, the table view will automatically include sections.
Alright thanks. So in my case would it just be sectionNameKeyPath:@"Section" or would it be sectionNameKeyPath:section. I've never tried sections before, so this is all new to me. Thank you!
mdhansen5 is offline   0 Reply With Quote
Old May 29, 2012, 10:17 AM   #4
forum user
macrumors regular
 
Join Date: Aug 2008
Quote:
Originally Posted by mdhansen5 View Post
Alright thanks. So in my case would it just be sectionNameKeyPath:@"Section"
Yes
__________________
Fuel Log Evo: The Cost Manager App for any Vehicle
forum user is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
core data, fetch, sections, sort, uitableview

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Getting web data into UITableView Tander iPhone/iPad Programming 7 Jul 30, 2013 12:37 AM
How to handle an empty UITableView Section w/ Core Data and FetchedResultsController mdhansen5 iPhone/iPad Programming 1 Jul 20, 2012 05:08 PM
Delete a row from Core Data UITableView with custom button mdhansen5 iPhone/iPad Programming 1 Jun 14, 2012 02:24 PM
iOS 5 - Core Data TableView Sections based on NSDate Ranges mdhansen5 iPhone/iPad Programming 1 Jun 13, 2012 01:16 AM
"See All" cell for UITableView sections. mdhansen5 iPhone/iPad Programming 5 Jun 7, 2012 01:59 PM

Forum Jump

All times are GMT -5. The time now is 05:22 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC