UA-17470720-3

Jump to content


Photo
- - - - -

SCTableViewModelDelegate Method Request


  • Please log in to reply
11 replies to this topic

#1 Tommy Wild

Tommy Wild

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 175 posts
Reputation: 12
Good

Posted 14 October 2010 - 03:34 PM

SCTableViewModelDelegate has:

- tableViewModel:willRemoveRowAtIndexPath:

but, can we get:

- tableViewModel:didRemoveRowAtIndexPath:

#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 15 October 2010 - 02:29 AM

Sure, will do! Thanks for your feedback Tommy :)

#3 Geoffrey Alexander

Geoffrey Alexander

    Sr. Member

  • STV 2.0
  • PipPipPip
  • 42 posts
Reputation: 0
Neutral

Posted 15 October 2010 - 04:34 AM

Yes! And also I wonder if STV could implement its own version of an existing UITableView delegate method:
(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
as this will help with a feature I'm build to make user-reordering of an arrayOfObjects section (created by STV automatically from an ArrayOfObjectCell selection) sticky. These all would be terrific.

#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 15 October 2010 - 05:04 AM

Hi Geoffrey,

Please note that SCArrayOfObjectsCell/SCArrayOfObjectSection automatically persists row movement by reordering the actual objects in the "items" array provided to SCArrayOfObjectsCell/SCArrayOfObjectsSection. You do not need to handle this yourself. This is exactly how the classes also handle row deletion by automatically deleting the associated item in the "items" array. Is this what you're after?

#5 Geoffrey Alexander

Geoffrey Alexander

    Sr. Member

  • STV 2.0
  • PipPipPip
  • 42 posts
Reputation: 0
Neutral

Posted 15 October 2010 - 06:00 AM

My objects are bound to Core Data so the user re-ordering is wiped clean each time the view is pushed again. Or is there a way to handle this in STV already?

The updated ordering also has to be written to each objects attribute "displayOrder" for use in other parts of the application, so I need to get the newly ordered array after the user reorders at the very least to walk through the objects and update the attribute. But I do want the reordering sticky for display as well.

#6 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 15 October 2010 - 06:17 AM

Core Data objects are actually stored in an NSSet, not an NSArray, and that's why they cannot be ordered (if you notice, Apple's Core Data templates always disable row movement).

How are you planning to persist the cell order if you were to do that manually?

#7 Tommy Wild

Tommy Wild

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 175 posts
Reputation: 12
Good

Posted 15 October 2010 - 06:22 AM

In the past I've used an entity attribute named "order" and used predicates to sort on that integer.

You could do the same and whenever the order is changed, just update the "order" attributes in that set to the order they are in the tableview (indexPath.row).

I think I remember seeing a topic on using predicates along with STV here somewhere.

#8 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 15 October 2010 - 06:36 AM

Thanks Tommy!

I think what we should do is add an "orderPropertyName" property to SCClassDefinition, and actually have STV automatically handle Core Data ordering if this property is appropriately set. What do you guys think?

Today is the day of new feature requests! (this being the sixth or seventh!) :)

#9 Geoffrey Alexander

Geoffrey Alexander

    Sr. Member

  • STV 2.0
  • PipPipPip
  • 42 posts
Reputation: 0
Neutral

Posted 15 October 2010 - 06:37 AM

To set the displayOrder attribute using the delegate method I mentioned, something like this:
- (void)tableView:(UITableView *)tableView 

moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath 

	  toIndexPath:(NSIndexPath *)destinationIndexPath;

{  

  NSMutableArray *things = [[fetchedResultsController fetchedObjects] mutableCopy];

 

  // Grab the item we're moving.

  NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath];

 

  // Remove the object we're moving from the array.

  [things removeObject:thing];

  // Now re-insert it at the destination.

  [things insertObject:thing atIndex:[destinationIndexPath row]];

 

  // All of the objects are now in their correct order. Update each

  // object's displayOrder field by iterating through the array.

  int i = 0;

  for (NSManagedObject *mo in things)

  {

	[mo setValue:[NSNumber numberWithInt:i++] forKey:@"displayOrder"];

  }

 

  [things release], things = nil;

  [managedObjectContext save:nil];

}
-- basically creating a temporary mutable array from the existing set order that reflects the user reordering. Walk the array to update the displayOrder attribute, and then you are ready for using that attribute when fetching the NSSet, like this:
- (NSFetchedResultsController *)fetchedResultsController

{

  if (fetchedResultsController) return fetchedResultsController;

 

  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

  NSEntityDescription *entity = 

			   [NSEntityDescription entityForName:@"ItemName" 

						   inManagedObjectContext:managedObjectContext];

 

  [fetchRequest setEntity:entity];

 

  NSSortDescriptor *sortDescriptor = 

			  [[NSSortDescriptor alloc] initWithKey:@"displayOrder" 

										  ascending:YES];

 

  NSArray *sortDescriptors = [[NSArray alloc] 

							  initWithObjects:sortDescriptor, nil];  

  [fetchRequest setSortDescriptors:sortDescriptors];

 

  NSFetchedResultsController *aFetchedResultsController = 

			  [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 

												  managedObjectContext:managedObjectContext

													sectionNameKeyPath:nil cacheName:@"ThingsCache"];

  aFetchedResultsController.delegate = self;

  [self setFetchedResultsController:aFetchedResultsController];

 

  [aFetchedResultsController release];

  [fetchRequest release];

  [sortDescriptor release];

  [sortDescriptors release];

 

  return fetchedResultsController;

}
-- in other words, getting the attribute value as the sort descriptor for the fetch request. Depending on where and when I can tie into STV via delegates to implement the code, it should work spiffy. And as I mentioned the resulting display order is important in my app when I later access the data for an output stream. I could also see how this might be baked into the next version of STV... :)

#10 Tommy Wild

Tommy Wild

    Advanced Member

  • STV 3.0 Pro
  • PipPipPipPipPip
  • 175 posts
Reputation: 12
Good

Posted 15 October 2010 - 06:38 AM

tarekskr wrote:

Thanks Tommy!

I think what we should do is add an "orderPropertyName" property to SCClassDefinition, and actually have STV automatically handle Core Data ordering if this property is appropriately set. What do you guys think?

Today is the day of new feature requests! (this being the sixth or seventh!) :)



Speaking for myself, I think that would be really great!

#11 Geoffrey Alexander

Geoffrey Alexander

    Sr. Member

  • STV 2.0
  • PipPipPip
  • 42 posts
Reputation: 0
Neutral

Posted 15 October 2010 - 06:43 AM

Yes! We could start called this Sensible CoreData! :laugh:

#12 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 15 October 2010 - 06:48 AM

Great guys! :) We'll implement this and get back to you ASAP.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users