I have an object (core data object) that this object have a property called "weeks" to define how many weeks ago this object was created.
Now im displaying this type of objects in a table view, and I created this table view as grouped table view and now I want to set the headers of the grouped table view by the number of weeks.
So
header:2 weeks ago
obj1
obj2
heaser:3 weeks ago
obj3
obj4
obj5
So I guess the magic happened in:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//Settings up the sections title
if(section == 0) {
return #"something";
} else {
return #"something";
}
}
So now I have this object called Target, and its weeks property (target.weeks).
How can i use it to set the headers grouped by number of weeks of the object?
thanksss
Since you are dealing with core data the best solution is to use NSFetchedResultsController. Apple has provided this class specifically for fetching core data objects and displaying them on table view and you can even mention the attribute based on which you wish to categorise your objects into sections.
In a non core data scenario one way is to create an NSDictionary with one entry for each section. Nothing stops you from using this technique for core data objects. But NSFetchedResultsController is much better. The key will be the section title and value will be an NSArray of objects that you wish to show in that section.
Your dictionary will look like {sec1:[row0Obj,row1obj], sec2:[row0Obj, row1Obj]}
NSDictionary *dataSourceDictionary; //Add data to this dictionary as explained above
- (NSDictionary *)entryForSection:(NSInteger)section {
NSArray *allSections = [dataSourceDictionary allKeys];
//sort the sections array here to make sure your setions appear in correct order
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"self" ascending:YES];
allSections = [allSections sortedArrayUsingDescriptors:#[sortDescriptor]];
NSString *sectionTitle = allSections[section];
NSArray *objectsInSection = dataSourceDictionary[sectionTitle];
return #{ sectionTitle : objectsInSection };
}
- (NSArray *)objectsForSection:(NSInteger)section {
NSDictionary *entry = [self entryForSection:section];
NSArray *objects = (NSArray *)entry.allValues.firstObject;
return objects;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return dataSourceDictionary.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self objectsForSection:section].count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSDictionary *entry = [self entryForSection:section];
NSString *title = entry.allKeys.firstObject;
return title;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *objectsForSection = [self objectsForSection:indexPath.section];
id objectForRow = objectsForSection[indexPath.row];
//Configure your cell using objectForRow
//[cell configureWithObject:object];
//return cell;
}
Related
i use below code to binding tableview cell by "contacts" entity
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context=nil;
id delegate=[[UIApplication sharedApplication]delegate];
if ([delegate performSelector:#selector(managedObjectContext)])
{
context=[delegate managedObjectContext];
}
return context;
}
- (void)viewDidAppear:(BOOL)animated
{
//fetching contact into tableview
NSManagedObjectContext *moc=[self managedObjectContext];
totalFetch=[[NSFetchRequest alloc]initWithEntityName:#"Contacts"];
// 4 - Sort it if you want
totalFetch.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)]];
_contacts=[[moc executeFetchRequest:totalFetch error:nil]mutableCopy];
[self.myTable reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//create and initializetion cell
static NSString *cellIdentifier=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
NSManagedObject *contact=[_contacts objectAtIndex:indexPath.row];
//implementing tableViewCells with Contact attribs
[cell.textLabel setText:[NSString stringWithFormat:#"%#",[contact valueForKey:#"name"]]];
[cell.detailTextLabel setText:[contact valueForKey:#"phoneNum"]];
UIImage *image = [UIImage imageWithData:[contact valueForKey:#"photo"]];
[cell.imageView setImage:image];
return cell;
}
now i want to add index and section to mytableview by first letter of "name" field in "contacts" entity, Please put your answers according to my code, thanks
You need to implement the following datasource methods for UITableView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
The first two are standard and implement the cells that are going to be displayed in the table view.
The third one is the number of sections the table view should be divided up into. For something like this I would use an 'alphabet array' i.e. an array with all the letters of the alphabet in e.g.:
NSMutableArray *alphabetArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 26; i++) {
char letter = 'A';
letter += i;
[alphabetArray addObject:[NSString stringWithFormat:#"%c", letter]];
}
You can return alphabetArray.count for the third datasource method.
The fourth method requests the section number for the section title, all you need to do here is return: [alphabetArray indexOfObject:title];.
The fifth method is asking for the title for a specific section so you can return: alphabetArray[section]; here.
Finally the sixth method is asking for all the titles in an array so you can just return alphabetArray here .
In order to get the number of rows in a section you need to break down your contacts array by starting letter. To do this you can use predicates e.g.
NSString *letter = alphabetArray[indexPath.section];
NSArray *filteredContacts = [contacts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"name BEGINSWITH[cd] %#", letter]];
return filteredContacts.count;
You can apply the same logic to get the contact to display in a cell:
NSString *letter = alphabetArray[indexPath.section];
NSArray *filteredContacts = [contacts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"name BEGINSWITH[cd] %#", letter]];
Contact *contact = filteredContacts[indexPath.row];
Hope this helps.
I have some JSON data that I am getting from my database. I can pull it fine and load it into my table view. my issue is separating my JSON data so I can section the tableview.
JSON
[{"id":"1","name":"steve mans","phoneNumber":"(559)123-4455","showNumber":"1","greenCard":"1","expiration":"2014-02-15","driver":"1","paid":"1","watch":"1"},
{"id":"2","name":"myself and me","phoneNumber":"(559)321-6784","showNumber":"1","greenCard":"1","expiration":"2013-10-18","driver":"0","paid":"0","watch":"2"},
{"id":"4","name":"tod bellesmithson","phoneNumber":"(559)678-3421","showNumber":"0","greenCard":"1","expiration":"2013-11-22","driver":"1","paid":"0","watch":"2"},
{"id":"3","name":"John Smith","phoneNumber":"(559)123-1234","showNumber":"1","greenCard":"0","expiration":"2013-10-08","driver":"0","paid":"1","watch":"3"},
{"id":"5","name":"greg smith","phoneNumber":"559 345-1234","showNumber":"1","greenCard":"1","expiration":"2013-10-08","driver":"0","paid":"1","watch":"3"}]
What I am trying to do is, separate this data into three sections in my tableview. So I thought create three different tables and load each table into a different section of the tableview. But the information is the same in each one (id, name, phone etc.) So I ended up with one table and added a column that designates what shift people work, 'watch'. So how do I separate the data by using the watch column, so in my tableview i will have:
section one
people who work night shift
section two
people who work morning
section three
night shift
Try with this code:
NSArray *data = (NSArray)[NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&error];
NSMutableArray *morningShift = [NSMutableArray array];
NSMutableArray *noonShift = [NSMutableArray array];
NSMutableArray *nightShift = [NSMutableArray array];
for (int i=0; i< [data count]; i++)
{
NSDictionary *item = data[i];
if (#"1" == item[#"watch"] )
{
[morningShift addObject:item];
} else if (#"2" == item[#"watch"] )
{
[noonShift addObject:item];
} else if (#"3" == item[#"watch"] )
{
[nightShift addObject:item];
}
}
try this
NSMutableArray *tableData = [NSMutableArray alloc] init];
NSArray* nameArr = [NSArray arrayWithObjects: #"1", #"2",#"3",nil];
for(int i=0; i<[nameArr count]; i++)
{
[tableData addObject:[jsonArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"watch = %#",[nameArr objectAtIndex:i]]] ];
}
TableView Delegate Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [tableData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[tableData objectAtIndex:section ] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell= nil;
//implement your logic to display the cell item;
NSArray sectionData = [tableData objectAtIndex:indexPath.section];
NSArray rowData = [sectionData objectAtIndex:indexPath.row];
return cell;
}
Please note i have not compiled the code. There is a chance of compilation error.
check for section in your cellForRowAtIndexPath method and load data accordingly,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//initialize cell
if (indexPath.section == 0){
// load data
}
return cell;
}
in my app the user adds cells to a tableView using coreData. This works quite well. But now I want the table view to have sections.
The viewController in which you add new cells look like:
#property (strong) NSManagedObject *travel;
...
-(void)viewDidLoad{
countryName = [[NSArray alloc] initWithObjects:
#"USA", #"England", #"Italy", nil];
countryLabel.text= [countryName objectAtIndex:[picker selectedRowInComponent:0]];
}
- (IBAction)save:(id)sender {
[self.travel setValue:countryLabel.text forKey:#"country"];
}
and in the viewController which displays the cells in a tableView:
#property (strong) NSMutableArray *travelAll;
...
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"position == %#",_positionString];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"Travel"];
[fetchRequest setPredicate : predicate ];
self.travelAll = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
...
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{ NSArray* headers = [NSArray arrayWithObjects:#"USA",#"England","Italy",nil];
return [headers objectAtIndex:section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.travelAll count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *travel = [self.travelAll objectAtIndex:indexPath.row];
...
return cell;
}
But now my tableView I have just this three sections (headers) but I can't add cell to them.
For example: the user selects USA for his new cell, so this cell should be displayed in the section USA
Your datasource methods are flawed. For example, you are returning the same cell data for each section. There are other problems as well.
It is much better to use a NSFetchedResultsController. Start from the Apple templates (you get that if you create a new project and choose "Use Core Data"), that employ the fetched results controller.
Your section design becomes very simple then: it is enough to simply specify the sectionNameKeyPath property.
I started develop an app which is using WCF service with JSON data. I got the data from WCF service but I didn't use it as I want.
here is the JSON data:
{"MenuDoldurAndroidResult":[
{"menu_description":"Turkish Pizza","menu_title":"L Pizza","menu_price":"26 TL"},{"menu_description":"Italiano Pizza","menu_title":"L Pizza","menu_price":"27 TL"},{"menu_description":"Extravaganza","menu_title":"L Pizza","menu_price":"29 TL"},{"menu_description":"Pepporoni Pizza","menu_title":"L Pizza","menu_price":"28 TL"},{"menu_description":"Turkish Pizza","menu_title":"S Pizza","menu_price":"12 TL"},{"menu_description":"Italiano Pizza","menu_title":"S Pizza","menu_price":"13 TL"},{"menu_description":"Extravaganza","menu_title":"S Pizza","menu_price":"15 TL"},{"menu_description":"Pepporoni Pizza","menu_title":"S Pizza","menu_price":"14 TL"}
]}
What I want:
If there are 2 title here, there must be 2 section in table view. Every item must be in their section.
Like this:
-L Pizzas
Turkish Pizza 26 TL
Italiano Pizza 27 TL
Extravaganza Pizza 29 TL
Pepperoni Pizza 28 TL
-S Pizzas
Turkish Pizza 12 TL
Italiano Pizza 13 TL
Extravaganza Pizza 15 TL
Pepperoni Pizza 14 TL
How can I access this item and display like this ?
- (void)viewDidLoad
{
[super viewDidLoad];
//I posted request to service here. I didn't write these parts of code.
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];
NSMutableArray *array= [json objectForKey:#"MenuDoldurAndroidResult"];
menu = [[NSMutableArray alloc] initWithCapacity:3];
NSString *descriptionTemp;
NSString *titleTemp;
NSString *priceTemp;
for(int i=0; i< array.count; i++)
{
NSDictionary *menuList= [array objectAtIndex:i];
titleTemp = [menuList objectForKey:#"menu_title"];
descriptionTemp = [menuList objectForKey:#"menu_description"];
priceTemp = [menuList objectForKey:#"menu_price"];
[menu addObject:[NSArray arrayWithObjects:titleTemp,descriptionTemp,priceTemp,nil]];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
-(NSString *)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
if (section==0) {
return #"L Pizzas";
}
else{
return #"S Pizzas";
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
cell.textLabel.text = [menu objectAtIndex: indexPath.row];
return cell;
}
If your content is static you can try using the answer by Sunny. But if is dynamic it's better to store the data in a different way. Obviously L pizza and S pizza seems to be a category and the rest are like category items.
You need to make a collection of the categories. Demo Project Source Code
- (void)viewDidLoad
{
[super viewDidLoad];
//I posted request to service here. I didn't write these parts of code.
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];
NSMutableArray *allPizzas = [json[#"MenuDoldurAndroidResult"] mutableCopy];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"menu_price"
ascending:YES
selector:#selector(compare:)];
[allPizzas sortUsingDescriptors:#[sortDescriptor]];
NSMutableArray *pizzaCategories = [#[]mutableCopy];
//Find unique categories in all the pizzas
NSSet* categories = [NSSet setWithArray: [allPizzas valueForKey:#"menu_title"]];
//Enumerate to form a new reformatted category array
for (NSString *categoryTitle in categories)
{
//Predicate is used to find the items that come under current category
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"menu_title == %#",categoryTitle];
NSArray *categoryItems = [allPizzas filteredArrayUsingPredicate:predicate];
//New dictionary with name of category and category items are formed
NSDictionary *categoryDict = #{#"menu_title":categoryTitle,#"pizzas":categoryItems};
[pizzaCategories addObject:categoryDict];
}
//Assign the new formatted category array to the instance variable for holding categories.
self.categories = pizzaCategories;
}
Modify the datasource of tableView for the new structure
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//The count of categories will give number of sections
NSUInteger sections = [self.categories count];
return sections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//The number of items in a category is calculated
NSDictionary *category = self.categories[section];
return [category[#"pizzas"] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
//Title for the category
NSDictionary *category = self.categories[section];
return category[#"menu_title"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSDictionary *category = self.categories[indexPath.section];
NSArray *categoryItems = category[#"pizzas"];
NSDictionary *categoryItem = categoryItems[indexPath.row];
cell.textLabel.text = categoryItem[#"menu_description"];
cell.detailTextLabel.text = categoryItem[#"menu_price"];
return cell;
}
You can also use the free Sensible TableView framework to fetch the data from the web service and automatically display it in your table view.
I have a Company entity with about 20 fields and I'm wanting to use a grouped tableView with manually created section headers (ie: General, Financial, Misc.), but I am unsure of how to make Core Data understand how to treat these section headers and make them relate only to the data I want to show in these groups.
For example, name, logo, etc would go under General, budgets, cash would go under financial, etc.
Basically, I want to control what data from Core data gets put into each category and display it.
In the Core books sample there is this code:
/*
The data source methods are handled primarily by the fetch results controller
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[fetchedResultsController sections] count];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
But how do I make it understand that the sections aren't in Core data, but created manually?
I've got an answer to my problem now, I do not know if its the correct approach, but it is working and would welcome comments.
Just to clarify what the problem was, and what I was trying to do.
I have a core data entity company with about 10 or so fields inside them, however rather than listing them all in one go, I wanted to group the outputted fields.
For example, I have about 6 fields relating to cash such as "cash", "marketingBudget", "seoBudget", etc and I wanted to group this data on the tableView, but the problem was I didn't know how to set up a relationship so that table.field.x belonged to group.x, and so on.
The answer I came to was to use a PLIST/dictionary that pretty much mirrors the structure of the core data entity; and assign the structure to the groups I want to display.
My dictionary looks like this;
(root)
->CompanyTpl (array)
--> Item 0 (Dictionary)
---> Section (String) = "General"
---> Children (Array
)
------> Item 0 (Dictionary)
----------> Key = "name"
----------> Value = "Company Name" ...
Where the Key would be a reference for Core Data to use and display its contents, if required.
Where the Value would be would to display at the cellForRowAtIndexPath.
So, in my code I basically went through the section (by which I mean tableView section) and then find the correlating children info from the PLIST; and get the Key/Value and use this as and when required.
Here is a cut down version of the code.
- (void)viewDidLoad {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"CompanyTpl" ofType:#"plist"];
self.companyDictionary = [[NSDictionary dictionaryWithContentsOfFile:plistPath] retain];
// self.tableDataSource is a NSMutableArray
self.tableDataSource = [self.companyDictionary objectForKey:#"CompanyTpl"];
// Debugging info
NSLog(#"Section = 0");
NSLog(#"%#", [self.tableDataSource objectAtIndex:0]);
NSLog(#"Section Name = %#", [[self.tableDataSource objectAtIndex:0] objectForKey:#"Section"]);
NSArray *sectionChildren = [[self.tableDataSource objectAtIndex:0] objectForKey:#"Data"];
NSLog(#"Section Children = %#", sectionChildren);
NSLog(#"Count of Section Children = %d", [sectionChildren count]);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ([self.tableDataSource count]);
}
// Section header
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title = nil;
title = [[self.tableDataSource objectAtIndex:section] objectForKey:#"Section"];
return title;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger rows = 0;
NSArray *sectionChildren = [[self.tableDataSource objectAtIndex:section] objectForKey:#"Data"];
NSLog(#"Section Children = %#", sectionChildren);
NSLog(#"Count of Section Children = %d", [sectionChildren count]);
rows = [sectionChildren count];
return rows;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *sectionChildren = [[self.tableDataSource objectAtIndex:indexPath.section] objectForKey:#"Data"];
NSDictionary *sectionChildrenData = [sectionChildren objectAtIndex:indexPath.row];
//NSLog(#"Section Children data = %#", sectionChildrenData);
NSString *scKey = [sectionChildrenData objectForKey:#"Key"];
NSString *scValue = [sectionChildrenData objectForKey:#"Value"];
NSLog(#"scKey = %#", scKey);
// Grab the data from Core Data using the scKey
static NSString *CellIdentifier = #"defaultCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
//cell.textLabel.text = #"test";
cell.textLabel.text = scValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
The idea would be that I can use the KEY when referencing Core Data to grab its contents and display it on the tableView controller at cellForRowAtIndexPath cell.textLabel.text value.
One could go a little further in depth and have more info in the PLIST such as what the subtitle should be, etc.
Anyway, would welcome comments and thoughts.
Thanks.
I have got a little closer to an answer, but I am still having trouble hooking up core data items so that they are in certain sections.
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//return [[fetchedResultsController sections] count];
//NSLog(#"%d", [self.sectionArray count] );
return 4;
}
// Section header
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title = nil;
switch (section) {
case 0:
title = #"General";
break;
case 1:
title = #"Finanical";
break;
case 2:
title = #"Category A";
break;
case 3:
title = #"Category B";
break;
case 4:
title = #"Misc";
break;
default:
break;
}
return title;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
/*
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
*/
NSInteger rows = 0;
// The number of rows depend on the section
switch (section) {
case 0:
rows = 2;
break;
case 1:
rows = 3;
break;
case 2:
rows = 4;
break;
default:
break;
}
return rows;
}
What this does is manually create 4 sections, the names don't matter at the moment, and then I create a different number of rows for each section.
So far, so good.
The problem I'm having is making Core Data understand that in section0.row0 I want the textLabel to say the the company name etc.
I'm thinking that all of this has to be in a dictionary, laying-out the entire structure that I want, and the labels to display; and then in cellForRowAtIndexPath I display the array within the dictionary that I want.
ie:
[root] CompanyTpl (array)
--> Item 0 (dictionary)
-----> Category (string) = "General"
-----> Data (Array)
---------> Item 0 (Dictionary)
---------------> cdFieldName: name
---------------> display: "Company name"
Where cdFieldName is a reference to the field name within Core Data I want to display.
If there is an alternative way to do this, I'd be interested in finding out.
Thanks.