Welcome, Guest
Username Password: Remember me

Accessing Core Data in willDisplayCell
(1 viewing) (1) Guest
  • Page:
  • 1

TOPIC: Accessing Core Data in willDisplayCell

Accessing Core Data in willDisplayCell 1 year, 8 months ago #1

I need to perform some configuration of a table cell and have seen that
- (void)tableViewModel:(SCTableViewModel *) tableViewModel willDisplayCell:(SCTableViewCell *) cell forRowAtIndexPath:(NSIndexPath *) indexPath {

should probably do the trick. However I cant seem to find out how to access the underlying managed object ( which was defined using SCClassDefinition) ?? I gather I can access it via the SCTableViewCell but cant work out which property and bound object is null. Any help wold be great.

Thanks.
  • monsta
  • OFFLINE
  • Expert Boarder
  • Posts: 130
  • Karma: 2

Re: Accessing Core Data in willDisplayCell 1 year, 7 months ago #2

Any help on this ?? At the root of my tableview, I have my table cells and usually I would do the following :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// ... code
Inspection *inspection = [self.fetchedResultsController objectAtIndexPath:indexPath];
// ... code
}

but cant figure out how to link into the core data behind a SCTableViewCell. Any help appreciated. Thanks!
  • monsta
  • OFFLINE
  • Expert Boarder
  • Posts: 130
  • Karma: 2
Last Edit: 1 year, 7 months ago by monsta.

Re: Accessing Core Data in willDisplayCell 1 year, 7 months ago #3

Hi Monsta:

Apologies, was away for the weekend

You should be able to access the managed object using the boundObject property of SCTableViewCell. Here is a code sample:

 
- (void)tableViewModel:(SCTableViewModel *)tableViewModel
willDisplayCell:(SCTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *managedObject = (NSManagedObject *)cell.boundObject;
...
// your code here
}
 


Is the code above not working for you? If it's not, what type of cell are you using? Would you please post some code for the class definition and what type of section are you using? Thanks.
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2404
  • Karma: 72

Re: Accessing Core Data in willDisplayCell 1 year, 7 months ago #4

Thanks for the update.

I have some code, but might be easier to explain it from the CoreData sample app point of view. The code is as :
SCClassDefinition *taskStepDef =
[SCClassDefinition definitionWithEntityName:@"TaskStepEntity" withManagedObjectContext:managedObjectContext
withPropertyNames: [NSArray arrayWithObjects:@"name", @"desc", nil]];
and the rest as per your example.

and the code for 'willDisplayCell' is as you stated. Nothing is printed out though after I add a new 'Task', implying the boundobject is null.
- (void)tableViewModel:(SCTableViewModel *)tableViewModel
willDisplayCell:(SCTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *managedObject = (NSManagedObject *)cell.boundObject;
NSLog(@" %@", managedObject);

// your code here
}


Am I missing something ?? Should that boundobject have data in it, since by default it uses the 'name' to show in the tablecell.
  • monsta
  • OFFLINE
  • Expert Boarder
  • Posts: 130
  • Karma: 2

Re: Accessing Core Data in willDisplayCell 1 year, 7 months ago #5

Yes, this behavior is correct. Cells automatically generated by an SCArrayOfObjectsSection do not have a bound object. Here is some sample code to illustrate how to access the bound object for these cells:

 
- (void)tableViewModel:(SCTableViewModel *)tableViewModel
willDisplayCell:(SCTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
SCArrayOfObjectsSection *section = (SCArrayOfObjectsSection *)[tableViewModel sectionAtIndex:indexPath.section];
NSManagedObject *managedObject = [section.items objectAtIndex:indexPath.row];
NSLog(@" %@", managedObject);
 
// your code here
}
 


Also, please note that you have two other SCTableViewModelDelegate methods that get called as a new object is being created:

1- tableViewModel:itemCreatedForSectionAtIndex:item: -This is called when the user taps the add button and the object is first created, but before actually being added to the section (user still has the chance to cancel operation).

2- tableViewModel:itemAddedForSectionAtIndexPath:item: -This is called when the user taps the Done button and the managed object is finally added to the section.

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

Re: Accessing Core Data in willDisplayCell 1 year, 7 months ago #6

Awesome!! Thank you so much or your help.!

That's exactly what I was after.
  • monsta
  • OFFLINE
  • Expert Boarder
  • Posts: 130
  • Karma: 2
  • Page:
  • 1
Time to create page: 1.76 seconds