Welcome, Guest
Username Password: Remember me

Adding a Property Programmatically
(1 viewing) (1) Guest
  • Page:
  • 1

TOPIC: Adding a Property Programmatically

Adding a Property Programmatically 1 year, 6 months ago #1

Hi,

I am using CoreData and I setup my class definition as follows :

SCClassDefinition *inspectionDef =
[SCClassDefinition definitionWithEntityName:@"Inspection" withManagedObjectContext:managedObjectContext withPropertyNames:[NSArray arrayWithObjects:@"address", @"suburb"]];

and then use the following to associate it to the tableModel :
tableModel = [SCArrayOfObjectsModel alloc] initWithTableView:self.tableView withViewController:self
withEntityClassDefinition:inspectionDef];

Once the initial "Inspection" data has been entered using a autogenerated detailview (and a ManagedObject created for it) I then want to add additional propertyNames i.e. "areas" ) and only then.

Is that possible in the current framework ?? I have tried doing the following in the "itemAddedForSectionAtIndexPath" delegate method :
BOOL result = [inspectionDef addPropertyDefinition:inspectorAreaPropertyDef];

but that raises exceptions. So it might just be that I'm using it incorrectly.
  • monsta
  • OFFLINE
  • Expert Boarder
  • Posts: 130
  • Karma: 2

Re: Adding a Property Programmatically 1 year, 6 months ago #2

Hi Phil,

All properties should be added to your class definition before passing it on to SCArrayOfObjectsModel. Is there any reason why you're not doing so?

Furthermore, I notice a bug with your code:

 
[NSArray arrayWithObjects:@"address", @"suburb"]
 


This will crash. You need to have the last object as nil:

 
[NSArray arrayWithObjects:@"address", @"suburb", nil]
 
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2404
  • Karma: 72

Re: Adding a Property Programmatically 1 year, 6 months ago #3

Hi,

Yeah that code I had copied and pasted from my actual code for brevity.

However, the reason I didn't want to include all the fields in my original class definition is that I do not want that all fields visible when the data is initially filled out in the table. I only wanted the "Areas" cell visible once the object had been initially saved.
  • monsta
  • OFFLINE
  • Expert Boarder
  • Posts: 130
  • Karma: 2

Re: Adding a Property Programmatically 1 year, 6 months ago #4

You could achieve what you want by removing the Areas cell from the newly created detail model. Here is some sample code (assuming you added all properties to the class definition):

 
- (void)tableViewModel:(SCTableViewModel *)tableViewModel
detailViewWillAppearForSectionAtIndex:(NSUInteger)index
withDetailTableViewModel:(SCTableViewModel *)detailTableViewModel
{
SCTableViewSection *section = [detailTableViewModel sectionAtIndex:0];
if([section isKindOfClass:[SCObjectSection class]])
{
SCObjectSection *objectSection = (SCObjectSection *)section;
SCTableViewCell *areasCell = [objectSection cellForPropertyName:"areas"];
if(areasCell)
{
NSUInteger areasCellIndex = [objectSection indexForCell:areasCell];
[objectSection removeCellAtIndex:areasCellIndex];
}
}
}
 


Hope this helps.
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2404
  • Karma: 72

Re: Adding a Property Programmatically 1 year, 6 months ago #5

Perfect!!! Thanks.
  • monsta
  • OFFLINE
  • Expert Boarder
  • Posts: 130
  • Karma: 2
  • Page:
  • 1
Time to create page: 0.94 seconds