UA-17470720-3

Jump to content


Photo
- - - - -

Sorting Problem


  • Please log in to reply
11 replies to this topic

#1 Ken Walker

Ken Walker

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 38 posts
Reputation: 2
Good

Posted 28 September 2011 - 10:04 AM

I've got a problem with sorting in STV. I have a client object which contains firstname and lastname string fields. The class definition contains the following:

clientClassDef.titlePropertyName=@"firstName;lastName";
clientClassDef.orderAttributeName=@"lastName;firstName";

When I go into the detail view for a client and edit either of these fields, when I return to the tableview the data is not resorted. I'm updating the underlying objects so when I leave the tableview, go somewhere else in the app and return to the tableview the data is properly sorted.

I've tried it without the
clientClassDef.orderAttributeName=@"lastName;firstName";

and it doesn't seem to make any difference

also tried
clientClassDef.orderAttributeName=@"lastName";

but that did not work either.

Am I missing something simple here?

#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 28 September 2011 - 02:05 PM

Hi Ken,

'orderAttributeName' is actually for enabling Core Data custom user sorting via rearranging the cells at runtime. 'keyAttributeName' is the right place to specify the attribute that should be used with automatic sorting.

Please tell me if you need more help.

#3 Ken Walker

Ken Walker

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 38 posts
Reputation: 2
Good

Posted 28 September 2011 - 02:25 PM

Couldn't find a 'keyAttributeName' property. Did find 'keyPropertyName',but that did not fix the issue

the docs say that 'keyPropertyName' is only for Core Data objects and mine are custom objects.

"The key of the entity associated with the definition. The key is usually used when a set of entity objects are sorted. By default, SCClassDefinition sets this property to the name of the first property in the entity. Note: Only applicable to Core Data entity class definitions"
  • Daniel Boice likes this

#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 28 September 2011 - 11:46 PM

Hi again Ken,

I apologize, thought you were using Core Data. If you're using your own custom objects array, then all you need to do is sort your NSMutableArray externally then set the SCArrayOfObjectsSection 'items' property to it. Please tell me if I correctly understand what you need.
  • Tarek likes this

#5 Ken Walker

Ken Walker

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 38 posts
Reputation: 2
Good

Posted 29 September 2011 - 04:47 PM

Cant' get it to work. Here's my code inside -(void)tableViewModel:(SCTableViewModel *) tableViewModel itemEditedForSectionAtIndexPath:(NSIndexPath *)indexPath item:(NSObject *)item

The sorted array has the objects sorted in the proper order but assigning that to to the items section does not work

SCArrayOfObjectsSection *objectsSection = (SCArrayOfObjectsSection *)indexPath.section;
		NSSortDescriptor *sortDescriptor;
		sortDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES] autorelease];
		NSArray *sortDescriptors=[NSArray arrayWithObject:sortDescriptor];
		
		
		NSArray *sortedArray=[ourClients sortedArrayUsingDescriptors:sortDescriptors];
		NSMutableArray *myArray=[NSMutableArray arrayWithArray:sortedArray];
		[objectsSection setItems:myArray];


#6 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 01 October 2011 - 10:52 PM

Hi Ken,

Please keep in mind that since the table view has been already rendered here, you'll need to call these two additional statements:
...
[objectsSection reloadBoundValues];
[tableViewModel.modeledTableView reloadData];  // or reloadSections...
...

Please tell me if this now works for you.

#7 Ken Walker

Ken Walker

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 38 posts
Reputation: 2
Good

Posted 01 October 2011 - 11:37 PM

That didn't seem to make a difference. I've included the whole method below. The call to [thisClient updateClientDatabase] is updating the sqlite database

-(void)tableViewModel:(SCTableViewModel *) tableViewModel  itemEditedForSectionAtIndexPath:(NSIndexPath *)indexPath item:(NSObject *)item {
	if(tableViewModel.tag==0 && clientIndex!=-1) {
		Client *thisClient=(Client *)item;
		thisClient.isDirty=YES;
		[thisClient updateClientDatabase];
		SCArrayOfObjectsSection *objectsSection = (SCArrayOfObjectsSection *)indexPath.section;
											   
												 
												
		NSSortDescriptor *sortDescriptor;
		sortDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES] autorelease];
		NSArray *sortDescriptors=[NSArray arrayWithObject:sortDescriptor];
		
		
		NSArray *sortedArray=[ourClients sortedArrayUsingDescriptors:sortDescriptors];
	   
		NSMutableArray *myArray=[NSMutableArray arrayWithArray:sortedArray];
		[objectsSection setItems:myArray];
		[objectsSection reloadBoundValues];
		[tableViewModel.modeledTableView reloadData];
		
			   
	}
}


#8 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 01 October 2011 - 11:50 PM

So what is it that's happening? Is the table view staying with the same order? Also, are you sure that 'myArray' itself is being sorted correctly? Thanks.

#9 Ken Walker

Ken Walker

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 38 posts
Reputation: 2
Good

Posted 02 October 2011 - 12:14 AM

Yes the table view is staying in the same order and yes I've checked that myArray has the objects in the newly sorted order.

If I completely leave the section of the app that uses STV - then come back in(which will reread from the db) then the items are in the proper order, it's just when I come back to the main table view from the detail view after having changed the value of lastname.

In case it matters I've included the code below where I set up the section

SCClassDefinition *clientClassDef = [SCClassDefinition definitionWithClass:[Client class] 
														   withPropertyNames:[NSArray arrayWithObjects:@"firstName",@"lastName",@"preferredLevel",@"preferredList",nil]];
	
   
	[clientClassDef propertyDefinitionWithName:@"firstName"].type = SCPropertyTypeTextField;
	[clientClassDef propertyDefinitionWithName:@"firstName"].required=YES;
	[clientClassDef propertyDefinitionWithName:@"lastName"].type=SCPropertyTypeTextField;
	[clientClassDef propertyDefinitionWithName:@"lastName"].required=YES;
	clientClassDef.titlePropertyName=@"firstName;lastName";
	
  
	
	
	SCPropertyDefinition *levelPropertyDef = [clientClassDef propertyDefinitionWithName:@"preferredLevel"];
	levelPropertyDef.type = SCPropertyTypeSelection;
	NSMutableArray *ourLevelStrings=[[NSMutableArray alloc] initWithObjects:@"One",@"Two", @"Three",nil];
	levelPropertyDef.attributes=[SCSelectionAttributes attributesWithItems:ourLevelStrings
													allowMultipleSelection:NO allowNoSelection:NO];
  
	levelPropertyDef.title=@"Preferred Level";
	SCPropertyDefinition *listPropertyDef=[clientClassDef propertyDefinitionWithName:@"preferredList"];
	listPropertyDef.type=SCPropertyTypeLabel;
	listPropertyDef.title = @"Preferred List";
	
	SCArrayOfObjectsSection *clientsSection=[SCArrayOfObjectsSection sectionWithHeaderTitle:nil withItems:ourClients withClassDefinition:clientClassDef];
	

	UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
	self.navigationItem.rightBarButtonItem = addButton;
	[addButton release];
	clientsSection.addButtonItem=self.navigationItem.rightBarButtonItem;
 
	
	[clientTableViewModel addSection:clientsSection];


#10 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 04 October 2011 - 11:12 AM

Hey Kin,

I've tested a lot of code very similar to yours and everything seems to be working properly at my side. Is this by any means a project that you can email me to test? (support[at]sensiblecocoa.com) If not, please at least email me the view controller's interface and implementation files. Thanks a lot!

#11 Ken Walker

Ken Walker

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 38 posts
Reputation: 2
Good

Posted 08 October 2011 - 09:50 PM

Tarek,

Sorry it's taken me so long to get back to you - been busy in other areas of the app.

I now have it working. You were correct it had to do with the underlying object sorting. While the objects were sorting correctly, I had an issue with properly assigning the sorted array to the items collection. I finally got that right and now all is working as desired.

Thanks so much for your help and patience.

Ken

#12 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 10 October 2011 - 04:22 PM

You're most welcome Ken, glad you've got everything figured out now :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users