
Sample Code requested...
#1
Posted 10 May 2011 - 10:44 AM
Many thanks..
David
#2
Posted 10 May 2011 - 12:01 PM
I don't have sample project to hand yet but I could help maybe outline the approach. Do you have specific issues in integrating the scope bar or are you looking for something general.
Regards
Carl
#3
Posted 10 May 2011 - 12:44 PM
// Instantiate the tabel model tableModel = [[SCArrayOfObjectsModel alloc] initWithTableView:self.tableView withViewController:self withEntityClassDefinition:cardDef]; tableModel.searchBar = self.searchBar; tableModel.searchPropertyName = @"frontWord"; tableModel.addButtonItem = self.navigationItem.leftBarButtonItem; tableModel.autoSortSections = TRUE;
I want to add a scope bar that links to an item called category.
I have added a scope bar to the IB design, but I don't know if that's the correct thing to do, nor do I know how to link it to the tableModel..
Thanks again for any help...
David
#4
Posted 10 May 2011 - 01:12 PM
Maybe you're ahead of me but just in case - as summarised in the attached image, for the easy bit I use IB for the setup. If you need to set the scope titles at run time just override with;
[tableModel.searchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"Category 1",@"Category 2",nil]];
Then for the hard bit I suggest maybe using the following delegate method to filter the fetch results by a selected category:
- (void)tableViewModel:(SCArrayOfItemsModel *)tableViewModel searchBarSelectedScopeButtonIndexDidChange:(NSInteger)selectedScope;

Hope this helps,
Carl
#5
Posted 10 May 2011 - 01:26 PM
I guess what I'm having trouble getting my head around is how to use the delegate method, and link the scope bar items to my "categories" field. These are set in IB just as shown in your diagram.
David
#6
Posted 10 May 2011 - 02:57 PM
@David: I guess what I'm having trouble getting my head around is how to use the delegate method
Since STV will automatically react to whatever is being entered into the search bar, it has to take over as the search bar's delegate. STV does however republish the search bar's delegates as part of its SCTableViewModelDelegate protocol. The method you should be using is "tableViewModel:searchBarSelectedScopeButtonIndexDidChange:".
Please tell me if you need any more help.
#7
Posted 10 May 2011 - 03:55 PM
Further to David's excellent request could you outline some code that could be used within the delegate's method to further filter the STV tableView. I'm especially interested to know if this is possible using NSPredicate, maybe of the form [NSPredicate predicateWithFormat:@"(SELF contains[cd] %@)", scopeText] or similar.
So glad David raised this topic - I'll be using the solution on my next project.
Looking forward to your reply,
Carl
#8
Posted 10 May 2011 - 04:02 PM
I am stuck with what parameters (showing my age) I should give to that method.
I am only here:
tableModel.searchBar = self.searchBar; [tableModel.searchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"Noun",@"Verb", nil]]; tableModel.searchPropertyName = @"frontWord";
- (void)tableViewModel:(SCArrayOfItemsModel *)tableViewModel searchBarSelectedScopeButtonIndexDidChange:(NSInteger)selectedScope; { }
I want to use a field called "category" to simply filter the search results with the above labels.
Category is defined as:
SCPropertyDefinition *categoryPropertyDef = [cardDef propertyDefinitionWithName:@"category"]; categoryPropertyDef.type = SCPropertyTypeSelection; categoryPropertyDef.attributes = [SCSelectionAttributes attributesWithItems:[NSArray arrayWithObjects:@"Noun", @"Verb", @"Other", nil] allowMultipleSelection:NO allowNoSelection:NO];
#9
Posted 10 May 2011 - 04:09 PM
David
#10
Posted 10 May 2011 - 10:20 PM
could you outline some code that could be used within the delegate's method to further filter the STV tableView. I'm especially interested to know if this is possible using NSPredicate
Sure! Actually STV fully utilizes the super powerful NSPredicate. I think the following code will answer both your and David's questions: (please make sure you have STV 2.0.5.4 or later for this to work)
- (void)tableViewModel:(SCArrayOfItemsModel *)tableViewModel searchBarSelectedScopeButtonIndexDidChange:(NSInteger)selectedScope { // Filter the model according to the category equivalent to the selected scope if([tableViewModel isKindOfClass:[SCArrayOfObjectsModel class]]) { SCArrayOfObjectsModel *objectsModel = (SCArrayOfObjectsModel *)tableViewModel; objectsModel.itemsPredicate = [NSPredicate predicateWithFormat:@"category == %d", selectedScope]; [objectsModel reloadBoundValues]; [objectsModel.modeledTableView reloadData]; } }
That's it! I've actually wrote the predicate's format string from memory, so it's not actually tested, but should hopefully work unless I made a silly mistake

#11
Posted 10 May 2011 - 10:48 PM
"setFormat" does not appear to be valid anymore... I'm struggling through the docs (and StackOverflow)..
ideas?
file://.../AddWordViewController.m: warning: Semantic Issue: Instance method '-setFormat:' not found (return type defaults to 'id')
#12
Posted 10 May 2011 - 10:52 PM
#13
Posted 10 May 2011 - 11:03 PM
One day, I'll get to 22.
Meantime.. OK, I get a good compile and the scope bar appears, but there is no impact of selecting a scope item.
Does it matter if the object in the nib is a search bar, or search bar with scope, or search bar with Controller?
(It's the second).
Thanks for bearing with me...
#14
Posted 10 May 2011 - 11:11 PM
Just a quick question before delving into deeper testing: are you sure the delegate method I wrote above is actually being called? I would think if there is something wrong with my predicate string then the table view should appear empty. Thanks!
#15
Posted 10 May 2011 - 11:21 PM
@interface AddWordViewController : UITableViewController <SCTableViewModelDelegate, SCTableViewModelDataSource> { UITableView *tableView; UITextField *textField; SCArrayOfObjectsModel *tableModel; UISearchBar *searchBar; }
Now I get nothing in the table view..
David
#16
Posted 11 May 2011 - 08:14 AM
Tarek, can you please post a more complicated solution - I need to justify the time I spent way off-track hunting down the filteredArray :laugh: Can't believe how Sensibly Simple the solution was. Awesome work - where else but STV could you create a fully working IOS Core Data App with searchBar and scopeBar functionality in only 5 mins - including coffee!
Thanks again David for posting this request. It wasn't something I needed for my current project but I was very interested in having the solution in my toolbox. Thanks to Tarek's objectsModel.itemsPredicate solution I've gone back to STV's Core Data sample app and within a few minutes was able to add a working scopeBar to the PeopleViewController's search bar to filter by gender. The predicate format I've used is rough & ready for testing only - it's works buts needs a final polish.
Here if it helps any:
- (void)tableViewModel:(SCArrayOfItemsModel *)tableViewModel searchBarSelectedScopeButtonIndexDidChange:(NSInteger)selectedScope { if([tableViewModel isKindOfClass:[SCArrayOfObjectsModel class]]) { SCArrayOfObjectsModel *objectsModel = (SCArrayOfObjectsModel *)tableViewModel; switch (selectedScope) { case 1: //Male objectsModel.itemsPredicate = [NSPredicate predicateWithFormat:@"title == %@", @"Mr"]; break; case 2: //Female objectsModel.itemsPredicate = [NSPredicate predicateWithFormat:@"title == %@ OR title == %@", @"Miss", @"Mrs"]; break; default: objectsModel.itemsPredicate = nil; break; } [objectsModel reloadBoundValues]; [objectsModel.modeledTableView reloadData]; } }
Thanks to all.
#17
Posted 11 May 2011 - 08:30 AM
I had to change the case structure so that it starts at 0, not 1.
It does bring up two more questions, that may be connected..
1. When I perform a search, I click search but the keyboard is not released.
2. In the Apple Sample app: TableSearch, there is a nice implementation where the scope bar only appears when a user starts a search. Is there a way to implement this in STV?
David
#18
Posted 11 May 2011 - 10:36 AM
Re case structure, in my quick knock-up I wanted to handle the first scope button as 'default' i.e. remove any scoping.
Understand what your saying about dismissing the keyboard after a search without losing the search results. That's a common topic in any iOS forum and maybe this and animating the scopeBar on search would be better served if raised in a separate topic in this forum.
I'm thinking my solution would be similar to Apple's contact app i.e. on starting a search, overlay the tableView to dim it out and prevent tableView selection. Set it up so that the keyboard is dismissed when the overlay is tapped. I'm sure you've seen this approach covered elsewhere.
It would be a relatively easy and very useful feature for STV don't you think - to have the tableView put into 'search mode' and animate the scopeBar down into place when using the searchBar - cancelling search mode when the tableView overlay is tapped...uhm there's a thought!
Best regards,
Carl
#19
Posted 11 May 2011 - 11:05 AM
It would be a relatively easy and very useful feature for STV don't you think - to have the tableView put into 'search mode' and animate the scopeBar down into place when using the searchBar - cancelling search mode when the tableView overlay is tapped...uhm there's a thought!
I'll wait to hear from Tarek before I pick up the Apple Contact method, but that seems like a great feature for STV.
Thanks Carl. I wish everyone would chip into these discussions, unless Tarek wishes otherwise.
David
- Tarek and Roberto Montesinos like this
#20
Posted 11 May 2011 - 11:32 AM
@Carl: Tarek, can you please post a more complicated solution
LOL! Thanks Carl for all the complements!

@David: 1. When I perform a search, I click search but the keyboard is not released.
We've actually left this without a default action and left it for the developer to decide what to do depending on their own desired application behavior. To dismiss the keyboard, just implement the SCTableViewModelDelegate method called "tableViewModelSearchBarSearchButtonclicked:" and call [tableViewModel.searchBar resignFirstResponder].
2. In the Apple Sample app: TableSearch, there is a nice implementation where the scope bar only appears when a user starts a search. Is there a way to implement this in STV?
Apple are actually using a UISearchDisplayController to achieve this behavior, and not a simple UISearchBar/UITableView combination. The problem with using a UISearchDisplayController with STV is that it demands that it becomes the UITableView's dataSource and delegate, which STV requires to override itself to do its magic. I haven't studied UISearchDisplayController's architecture in enough depth yet, but I am guessing that we could easily make it work with STV if we subclass it into a new SCSearchDisplayController for example. I think UISearchDisplayController provides a lot of really cool visual cues, and if I am right about the subclassing theory, then we should probably have SCSearchDisplayController ready for STV 2.1. How urgent do you need this David?
I wish everyone would chip into these discussions, unless Tarek wishes otherwise.
I would actually LOVE to!! I was actually just discussing another topic with Carl (off the forums) regarding adding a new important feature to STV, and I'll be posting this for open discussion since we need to collect as much feedback as possible from all you guys before we start implementing it. To help have more people chip in, we're updating the website to have these kind of announcements appear visibly on the right column so that they would have better exposure.
Thanks a lot guys for all your ideas and feedback!!

1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users