Trying to implement UISearchBar into UINavigationBar my result tableview position become wrong (pic 2):
I've spent a couple of days to solve this issue, but there is no success )
Thanks!
EDITED
In my viewDidLoad:
[self.searchDisplayController.searchBar setPlaceholder:NSLocalizedString(#"SEARCH_PLACEHOLDER", nil)];
[self.searchDisplayController setDisplaysSearchBarInNavigationBar:YES];
My SearchDisplayController Delegate methods:
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
//filter work repfectly
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"not (isFav == 100)"];
[_fetchedResultsController.fetchRequest setPredicate:resultPredicate];
[self performFetch];
}
Related
On editing the Search Bar in UISearchDisplayController, it repeats the first typed letter recursively until the second letter pressed.
Here is what happens:
Please suggest any possible solution.
Code:
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContent:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)SearchBar
{
[SearchBar resignFirstResponder];
SearchBar.text = #"";
[self.tableView reloadData];
}
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
controller.searchBar.showsCancelButton = YES;
}
-(void)filterContent:(NSString*)searchText scope:(NSString*)scope
{
_searchArray = [[NSArray alloc] init];
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:[NSPredicate predicateWithFormat:#"SELF.user_name contains[c] %#", searchText], [NSPredicate predicateWithFormat:#"SELF.country_name contains[c] %#", searchText], [NSPredicate predicateWithFormat:#"SELF.city contains[c] %#", searchText], nil]];
_searchArray = [_users filteredArrayUsingPredicate:predicate];
}
#JananiM I think you having problem in this line [self filterContent:searchString...] which u assigned in your delegate method.
Try this one
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString
scope:[self.searchDisplayController.searchBar scopeButtonTitles][[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
i have implemented your code (which you posted above) in my application.I didn't get anything like this.I have checked breakpoint too.Check this
Hey guys I simply can't solve this problem i've got.
http://i.imgur.com/LjliN7y.png
I have tried setting bounds of the UITableView like so:
[tableView setBounds:CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, 320, self.view.size.height)];
That didn't seem to fix my problem(See picture above), I thought maybe it was some clipping under the settings of the search bar which it wasn't.
I also tried setting the size of the UITableView that didn't help me.
It's also weird when using the UITableView without searching it works fine and the cells go full size.
But as soon as i search it starts clipping like that.
I've tried searching on the internet for a solution, i don't seem to find anything.
here is the rest of my search code:
-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
UIButton *cancelButton;
UIView *topView = controller.searchBar.subviews[0];
for (UIView *subView in topView.subviews) {
if ([subView isKindOfClass:NSClassFromString(#"UINavigationButton")]) {
cancelButton = (UIButton*)subView;
}
}
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSString *predicateString = [NSString stringWithFormat:#"SELF beginsWith[cd] '%#'", searchText];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:predicateString];
searchResults = [combine filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
Check size of the table view for the UISearchDisplayController:
NSLog(#"%#",[self.searchDisplayController.searchResultsTableView description]);
I am trying to create a custom popup menu in a tableview. As I understand it I can should be able to do so by calling the [NSPopupButtonCell setView:myView] method passing in the custom view (which is just a NSView with an NSOutlineView in it).
So I have created a NSPopupButtonCell subclass and during initialisation I call setView and pass in the custom outline view..
EDIT
In IB I have set the table columns cell to a Popup Button Cell and set
the class to my custom LookupPopupButtonCell.
I still don't get my custom view displaying but my custom classes
initialisation methods appear to be getting called.
I have since replaced this approach with using a NSTableViewDelegate method dataCellForTableColumn. Now the popup shows my custom tableView.
Still no joy in getting the NSOutlineViewDelegate methods called though.
EDIT
OK I have managed to get things working using a NSPopupButton on a view. delegate works find and table view displays things fine. Seems that using NSPopupButtonCell the delegate methods never get called.
#implementation LookupPopupButtonCell
- (id)init
{
LOG(#"init called");
self = [super init];
if (self) {
[self initialise];
}
return self;
}
- (void)initialise
{
LOG(#"initialise called");
[self setFont:[NSFont fontWithName:#"System Regular" size:11]];
[self setBordered:NO];
[self setBezeled:NO];
// Set the Task Lookup Popup Menu
NSMenu *newLookupMenu = [[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:#"Custom"];
NSMenuItem *newItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:#"Lookup" action:nil keyEquivalent:#""];
[newItem setEnabled:YES];
TaskLookupViewController *viewController = [[TaskLookupViewController alloc] initWithNibName:#"TaskLookupViewController" bundle:nil];
[newItem setView:[viewController view]];
[newLookupMenu addItem:newItem];
[newItem release];
[self setMenu:newLookupMenu];
}
#end
#implementation TaskLookupViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
[self initialise];
}
return self;
}
- (void)awakeFromNib{
LOG(#"awakeFromNib called...");
[self viewDidLoad];
}
- (void)viewDidLoad {
FLOG(#"viewDidLoad called for %#", self);
/*
FLOG(#" _outlineView is %#", _outlineView);
FLOG(#" _outlineView is %#", [_outlineView identifier]);
FLOG(#" _outlineView delegate is %#", [_outlineView delegate]);
FLOG(#" _outlineView dataSource is %#", [_outlineView dataSource]);
*/
[_outlineView setDataSource:self];
[_outlineView setDelegate:self];
[_outlineView reloadData];
[_outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
[_outlineView setNeedsDisplay];
[_outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:2] byExtendingSelection:NO];
/*
FLOG(#" _outlineView delegate is %#", [_outlineView delegate]);
FLOG(#" _outlineView dataSource is %#", [_outlineView dataSource]);
*/
//NSTableColumn *tableColumn = [[_outlineView tableColumns] objectAtIndex:0];
//LOG(#" setting bindings");
//[tableColumn bind: #"value" toObject: _treeController withKeyPath: #"arrangedObjects.displayName" options: nil];
}
- (void)initialise {
LOG(#"initialise called");
_topLevelItems = [[NSArray arrayWithObjects:#"Project", #"Tasks and Deliverables", #"Finance", nil] retain];
_childrenDictionary = [NSMutableDictionary new];
[_childrenDictionary setObject:[NSArray arrayWithObjects:#"Scope", nil] forKey:#"Project"];
//[_childrenDictionary setObject:[NSArray arrayWithObjects:#"Issues", #"Risks", nil] forKey:#"Quality"];
[_childrenDictionary setObject:[NSArray arrayWithObjects:#"WBS", #"Functions", nil] forKey:#"Tasks and Deliverables"];
[_childrenDictionary setObject:[NSArray arrayWithObjects:#"Expenses", #"Ongoing Costs", #"Timesheets", nil] forKey:#"Finance"];
//[_childrenDictionary setObject:[NSArray arrayWithObjects:#"Applications", #"Interfaces", nil] forKey:#"IT Systems"];
//[_childrenDictionary setObject:[NSArray arrayWithObjects:#"People", #"Setup", nil] forKey:#"Administration"];
}
- (NSArray *)_childrenForItem:(id)item {
LOG(#"_childrenForItem called");
NSArray *children;
if (item == nil) {
children = _topLevelItems;
} else {
children = [_childrenDictionary objectForKey:item];
}
//FLOG(#" children are %#", children);
return children;
}
#end
#implementation TaskLookupViewController (NSOutlineViewDataSource)
- (void)outlineViewSelectionDidChange:(NSNotification *)notification {
LOG(#"outlineViewSelectionDidChange: called");
if ([_outlineView selectedRow] != -1) {
NSObject *item = [_outlineView itemAtRow:[_outlineView selectedRow]];
if ([_outlineView parentForItem:item] != nil) {
// Only change things for non-root items (root items can be selected, but are ignored)
FLOG(#" selected item is %#", item);
}
}
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
FLOG(#"outlineView:child:ofItem: called for item %#", item);
return [[self _childrenForItem:item] objectAtIndex:index];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
LOG(#"outlineView:isItemExpandable: called");
if ([outlineView parentForItem:item] == nil) {
return YES;
} else {
return YES;
}
}
- (NSInteger) outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
LOG(#"outlineView:numberOfChildrenOfItem: called");
FLOG(#" children count is %d", [[self _childrenForItem:item] count]);
return [[self _childrenForItem:item] count];
}
#end
#implementation TaskLookupViewController (NSOutlineViewDelegate)
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
LOG(#"outlineView:isGroupItem: called");
return [_topLevelItems containsObject:item];
}
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
LOG(#"willDisplayCell called");
[cell setTitle:#"Cell Title"];
}
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
LOG(#"outlineView:viewForTableColumn called");
// We just return a regular text view.
if ([_topLevelItems containsObject:item]) {
NSTextField *result = [outlineView makeViewWithIdentifier:#"HeaderTextField" owner:self];
// Uppercase the string value, but don't set anything else. NSOutlineView automatically applies attributes as necessary
NSString *value = [item uppercaseString];
[result setStringValue:value];
return result;
} else {
NSTextField *result = [outlineView makeViewWithIdentifier:#"ItemTextField" owner:self];
[result setStringValue:value];
return result;
}
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldExpandItem:(id)item;
{
LOG(#"outlineView:shouldExpandItem: called");
return YES;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
{
LOG(#"outlineView:shouldSelectItem: called");
return YES;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldCollapseItem:(id)item;
{
LOG(#"outlineView:shouldCollapseItem: called");
return NO;
}
#end
#implementation TaskLookupViewController (NSTableViewDelegate)
- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
LOG(#"tableView:dataCellForTableColumn:row: called");
NSString *identifier = [tableColumn identifier];
if ([identifier isEqualToString:#"task"]) {
//LOG(#" task column setting the cell");
LookupPopupButtonCell *cellView = [[LookupPopupButtonCell alloc] init];
return cellView;
}
NSTextFieldCell *cellView = [tableView makeViewWithIdentifier:#"hoursCell" owner:self];
return cellView;
}
#end
It seems you must use a VIEW based tableView to get the delegate messages. Blah, now to figure out how to bind one of them to Core Data, hopefully its not too hard !
Is there any way to reuse the same menu for each row ? I guess as long as the dataSource is not recreated each time its probably not too bad, still there could be lots of rows in this hierarchy!
I hide the searchViewController.searchResultsTableView to display the results in the main UITableView instead. But the self.tableView = searchDisplayController.searchResultsTableView so the mainTableView is unscrollable when typing search text or after "Search" is hit. How can I make the self.tableView represent the mainTableView?
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
self.searchDisplayController.searchResultsTableView.hidden=YES;
return YES;
}
Hey guys, i want to know why my SearchBar dont excludes the table view cell when i search again? It just add another cell above the first.
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
self.theTableView.allowsSelection = NO;
self.theTableView.scrollEnabled = NO;
[theTableView setRowHeight:110];
}
- (void)searchDisplayController:(UISearchDisplayController *)controller
willShowSearchResultsTableView:(UITableView *)tableView
{
[tableView setRowHeight:[[self theTableView] rowHeight]];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableData removeAllObjects];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=#"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
return NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// All functions goes here and bla bla bla
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
//Remove tudo da table e recarrega
[self.tableData removeAllObjects];
[theTableView reloadData];
[[[self searchDisplayController] searchResultsTableView] performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
}
Thanks
Please using a technic, which posted here with 2 tableviews and two fetch controllers.
it's a little bit complicated, but u can remove part of code, which u don.t need.