How to dismiss UISearchBar once segued - ios

Creating a search view, this is the code that i did for the main search view.
What happens is if i dont search/filter, the uisearchbar gets dismissed when segueing. but if i search/filter then the uisearchbar stays on the nav bar when seguing.
- (void)viewDidLoad {
[super viewDidLoad];
// There's no transition in our storyboard to our search results tableview or navigation controller
// so we'll have to grab it using the instantiateViewControllerWithIdentifier: method
UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:#"CompanySearchResultsNavigationController"];
// Our instance of UISearchController will use searchResults
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
// The searchcontroller's searchResultsUpdater property will contain our tableView.
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
// The searchBar contained in XCode's storyboard is a leftover from UISearchDisplayController.
// Don't use this. Instead, we'll create the searchBar programatically.
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.navigationItem.titleView = self.searchController.searchBar;
self.definesPresentationContext = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.objects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
CompanySearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"searchCell" forIndexPath:indexPath];
cell.productImageView.file = (PFFile *)object[#"profileImage"];
cell.productImageView.layer.cornerRadius = cell.productImageView.frame.size.width / 2;
cell.productImageView.clipsToBounds = YES;
[cell.productImageView loadInBackground];
cell.companyNameLabel.text = object[#"username"];
return cell;
}
#pragma mark - UISearchControllerDelegate & UISearchResultsDelegate
// Called when the search bar becomes first responder
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
// Set searchString equal to what's typed into the searchbar
NSString *searchString = self.searchController.searchBar.text;
[self updateFilteredContentForAirlineName:searchString];
// If searchResultsController
if (self.searchController.searchResultsController) {
UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController;
// Present SearchResultsTableViewController as the topViewController
CompanySearchResultsTableViewController *vc = (CompanySearchResultsTableViewController *)navController.topViewController;
// Update searchResults
vc.searchResults = self.searchResults;
// And reload the tableView with the new data
[vc.tableView reloadData];
}
}
// Update self.searchResults based on searchString, which is the argument in passed to this method
- (void)updateFilteredContentForAirlineName:(NSString *)companyName
{
if (companyName == nil) {
// If empty the search results are the same as the original data
self.searchResults = [self.objects mutableCopy];
} else {
NSMutableArray *searchResults = [[NSMutableArray alloc] init];
// Else if the airline's name is
for (PFObject *company in self.objects) {
if ([company[#"username"] containsString:companyName]) {
// NSString *str = [NSString stringWithFormat:#"%#", company[#"username"]];
// [searchResults addObject:str];
PFObject *searchedObject = company;
[searchResults addObject:searchedObject];
NSLog(#"Searched: %#",searchedObject[#"username"]);
}
self.searchResults = searchResults;
}
}
}

In your AirlineTableViewController, override [UIViewController prepareForSegue]:
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender {
[self.searchController setActive:NO];
}

Related

SearchController not searching custom UITableViewCells

Not sure if the title makes to much sense so here is a description of what is going on:
I have a UITableViewController that is using a custom UITableViewCell for its data. I am then manually adding a searchBar to the header of the UITableView and setting it up based on this tutorial : http://useyourloaf.com
Now the searchBar is setup, it looks like it is working but the issue is that I am not actually getting any results and the table is not loading properly (the search results, it load the base data fine)
Here is my code for comparison. I know I must be missing something simple...
** I am hardcoding the data for my cells at the moment, this will change to a core data model once I can fix this issue... although this may be the base of my issue as I am hardcoding the cells at each IP **
#implementation CharitiesTableViewController{
NSArray *charities;
NSArray *searchResults;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setColors];
[_charityTable registerNib:[UINib nibWithNibName:#"CharityTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"charityCell"];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 350.0;
// self.tableView.contentInset = UIEdgeInsetsMake(-2.0f, 0.0f, 0.0f, 0.0);
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.searchController.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void) setColors {
}
#pragma mark - Search Controller
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString];
[self.tableView reloadData];
}
- (void)searchForText:(NSString *)searchText {
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"name contains[c] %#", searchText];
searchResults = [charities filteredArrayUsingPredicate:resultPredicate];
}
- (void)willPresentSearchController:(UISearchController *)searchController {
self.navigationController.navigationBar.translucent = YES;
}
-(void)willDismissSearchController:(UISearchController *)searchController {
self.navigationController.navigationBar.translucent = NO;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == _charityTable)
{
return 1;
}
return [searchResults count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
return SectionSpacer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return SectionSpacer;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0:{
CharityTableViewCell *cell = (CharityTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"charityCell"];
cell.charityImage.image = [UIImage imageNamed:#"cleanup"];
cell.charityName.text = #"Garbage cleanup - Crowchild";
cell.charityTagLine.text = #"City of Calgary";
cell.charityDescriptionShort.text = #"We are rounding up anyone that wants to help clean up the grass and nearby areas close to crowchild.";
return cell;
}
default:{
UITableViewCell *cell;
return cell;
}
}
}
#end
Thanks for you help!
I am silly.... I was trying to do this without initializing the data like a com mentor said. The code is correct and if anyone has any similar issues make sure your array of data actually has data in it...

iOS adding a UISearchController is unhiding the NavigationBar

I am having this weird side effect when I use a UISearchController to update my table view (If I select something from the tableview without searching the bug doesn't manifest itself). But when I search, select a cell, and then popViewControllerAnimated: for some reason the NavigationBar is no longer hidden. I want to think that this is a bug within iOS and not specific to my code. But I thought I would see if anyone can spot a bug in my code or has any ideas of something I might be doing wrong. I've added [self.navigationController setNavigationBarHidden:YES]; to my viewWillAppear of the rootView but the bar doesn't go away until the animation is over.
My TableView/UISearchController code:
#interface LBSelectUniversityView()<UISearchResultsUpdating, UISearchBarDelegate>
#property (strong, nonatomic) UISearchController *searchController;
#end
#implementation LBSelectUniversityView {
NSArray *schoolNames;
NSArray *searchResults;
}
- (void)viewDidLoad {
[super viewDidLoad];
schoolNames = [[LBUtilities sharedInstance] schoolNames];
searchResults = schoolNames;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return searchResults.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
...
return cell;
}
- (void)filterContentForSearchText:(NSString*)searchText{
if ([searchText isEqualToString:#""]) return;
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"SELF contains[cd] %#",
searchText];
searchResults = [schoolNames filteredArrayUsingPredicate:resultPredicate];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *searchString = searchController.searchBar.text;
[self filterContentForSearchText:searchString];
[self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
[self.navigationController popViewControllerAnimated:YES];
}
#end
Does the problem go away if you set searchController.hidesNavigationBarDuringPresentation = NO?
Maybe the following is happening:
When you start searching, searchController.active is set to YES. Thus searchController calls [... setNavigationBarHidden:YES] because UISearchController.hidesNavigationBarDuringPresentation = YES by default.
popViewControllerAnimated: is called.
searchController.active is set to NO, so searchController calls [... setNavigationBarHidden:NO]. This causes the navigation bar to be shown.
I think once your search results comes back you are reloading your main table and not search table. This is how you should load data on search results table.
self.searchController.searchResultsTableView reloadData];

Present UISearchController after button tapped

I am looking for a way how to present a search view with search bar after button is tapped. Am I suppose to create a new UIView with search bar and then after button is tapped segue to that UIView? Or is there a better option?
You can imagine that as searching in Youtube app but you would see search results while typing.
EDIT:
So now I managed to present search bar after button is tapped but it is not displaying correctly. Problem is when the search bar is moved lower it is being cut from down:
My code:
implementation ViewController {
NSInteger listNo;
DataClass *dataClass;
UITableViewController *searchResultsController;
}
- (void)viewDidLoad {
[super viewDidLoad];
dataClass = [DataClass getInstance];
self.navigationController.navigationBarHidden = YES;
// Prepare Search Controller and Result Controller
searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y + 5, self.searchController.searchBar.frame.size.width, 44.0);
[self.searchController.searchBar sizeToFit];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = #"Ahoj";
return cell;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
}
- (void)didDismissSearchController:(UISearchController *)searchController {
}
// Load data to next view
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showTable"]) {
AdditivesTableViewController *atvc = (AdditivesTableViewController *)segue.destinationViewController;
switch (listNo) {
case 0:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.nonDangerousAdditives];
break;
case 1:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.mediumDangerousAdditives];
break;
case 2:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.dangerousAdditives];
break;
case 3:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.highDangerousAdditives];
break;
default:
atvc.additivesList = [[NSMutableArray alloc] initWithArray:(NSArray *)dataClass.allAdditives];
break;
}
}
}
#pragma mark - IBActions
- (IBAction)nonDangList:(id)sender {
listNo = 0;
[self performSegueWithIdentifier:#"showTable" sender:self];
}
- (IBAction)medDangList:(id)sender {
listNo = 1;
[self performSegueWithIdentifier:#"showTable" sender:self];
}
- (IBAction)dangList:(id)sender {
listNo = 2;
[self performSegueWithIdentifier:#"showTable" sender:self];
}
- (IBAction)highDangList:(id)sender {
listNo = 3;
[self performSegueWithIdentifier:#"showTable" sender:self];
}
- (IBAction)searchAdditive:(id)sender {
[self.view addSubview:self.searchController.searchBar];
[self.searchController setActive:YES];
[self.searchController.searchBar becomeFirstResponder];
}
#end

List not being populated from other view

I have a view controller where the user can populate an item. Upon exiting the view that object should be saved, and loaded by the previous view which contains a list of all these objects. My NSLog shows that the object is being saved, but I can't get it to appear in the list view.
Code for both controllers is below. Thanks
List Controller
#import "ItemsViewController.h"
#import "Calculation.h"
#import "CalculationItemStore.h"
#import "CalculationDetailViewController.h"
#interface ItemsViewController()
#property UISegmentedControl *segment;
#end
#implementation ItemsViewController
- (instancetype)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
UINavigationItem *navItem = self.navigationItem;
navItem.title = #"MACS";
// Create a new bar button item that will send
// addNewItem: to CalculationsViewController
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addNewItem:)];
// Set this bar button item as the right item in the navigationItem
navItem.rightBarButtonItem = bbi;
navItem.leftBarButtonItem = self.editButtonItem;
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)viewDidLoad
{
self.segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Publication", #"About", nil]];
self.tableView.tableHeaderView = _segment;
[_segment addTarget:self action:#selector(segmentPressed:) forControlEvents:UIControlEventValueChanged];
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:#"UITableViewCell"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[CalculationItemStore sharedStore] allCalculations] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get a new or recycled cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell" forIndexPath:indexPath];
// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the tableview
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *item = items[indexPath.row];
cell.textLabel.text = item.title;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CalculationDetailViewController *detailViewController = [[CalculationDetailViewController alloc] init];
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *selectedItem = items[indexPath.row];
// Give detail view controller a pointer to the item object in row
detailViewController.calculation = selectedItem;
// Push it onto the top of the navigation controller's stack
[self.navigationController pushViewController:detailViewController
animated:YES];
}
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If the table view is asking to commit a delete command...
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *item = items[indexPath.row];
[[CalculationItemStore sharedStore] removeItem:item];
// Also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void) tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath
{
[[CalculationItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row
toIndex:destinationIndexPath.row];
}
- (void)segmentPressed:(id)sender {
if (_segment.selectedSegmentIndex ==0) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://ferm.forestry.oregonstate.edu/facstaff/leshchinsky-ben"]];
}else if(_segment.selectedSegmentIndex ==1){
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [UIImage imageNamed:#"MACSLoad#2x.png"]];
[self.view addSubview: imageView];
sleep(5);
imageView.hidden = YES;
}
}
- (IBAction)addNewItem:(id)sender
{
// Create a new Calculation and add it to the store
Calculation *newItem = [[CalculationItemStore sharedStore] createCalculation];
CalculationDetailViewController *detailViewController = [[CalculationDetailViewController alloc]initForNewItem:YES];
detailViewController.calculation = newItem;
detailViewController.dismissBlock = ^{
[self.tableView reloadData];
};
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:detailViewController];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
// [self presentViewController:navController animated:YES completion:NULL];
[self.navigationController pushViewController:detailViewController animated:YES];
}
#end
Item controller save method
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//clear responder
[self.view endEditing:YES];
//save changes
BOOL success = [[CalculationItemStore sharedStore]saveChanges];
if(success){
NSLog(#"Saved all calcs");
}else{
NSLog(#"failure saving");
}
}
I think the issue is that by the time the table reloadData is called on the first controller, the second one is not done loading. You could notify the first VC to reload the data once the second one is done saving It can be notified using
create a delegate on the second one or
using notifications
Let me know if you need more details on how to do this!
Looks like there is also an issue with the object creation.
your calculation elements are not getting created properly. When you are ready to save.. look at the contents of self.privateItems. They values filled in are nil. You will need to instantiate the Strings properly.
engineerName needs to be alloc'd and other strings as well.
and when user hits Done, the values from the text boxes need to be set to the your data model.

uisearchbar doesn't work

I created a subClass of UITextView with a searchBar, here is the code:
#import "SezioniTableController.h"
#interface SezioniTableController ()
#end
#implementation SezioniTableController
#synthesize searchBar,searchDisplayController;
#synthesize arraySezioni,arrayFiltrato;
#synthesize objTesto;
- (id)initWithStyle:(UITableViewStyle)style andArray:(NSMutableArray *)array{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.searchBar.delegate = self;
self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.arraySezioni = array;
self.arrayFiltrato = array;
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) loadView {}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.arrayFiltrato.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:#"MyIdentifier"];
}
self.objTesto = [arrayFiltrato objectAtIndex:indexPath.row];
cell.textLabel.text = self.objTesto.titoloTesto;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
NSDictionary *dict = [NSDictionary dictionaryWithObject:[self.arrayFiltrato objectAtIndex:indexPath.row] forKey:#"Testo"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"PassaggioTesto" object:self userInfo:dict];
NSLog(#"Click");
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return self.searchBar; //in .h, IBOutlet UISearchBar* search;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44;
}
- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContentForSearchText:searchString scope: [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
NSLog(#"aa");
//[[NSNotificationCenter defaultCenter] postNotificationName:#"ReloadTable" object:self];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
NSLog(#"ab");
//[[NSNotificationCenter defaultCenter] postNotificationName:#"ReloadTable" object:self];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void) filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
NSLog(#"a count:%d",self.arraySezioni.count);
[self.arrayFiltrato removeAllObjects]; // First clear the filtered array.
for (objTesto in self.arraySezioni){
NSComparisonResult result = [objTesto.titoloTesto compare:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame){
NSLog(#"Titolo: %#",objTesto.titoloTesto);
[self.arrayFiltrato addObject:self.objTesto];
}else{
NSLog(#"Non trovato");
}
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)saearchBar {
[self.arrayFiltrato removeAllObjects];
[self.arrayFiltrato addObjectsFromArray: self.arraySezioni];
}
#end
On my uiviewcontroller I create a table using the uitableview created with this code:
self.arrayTesti = [self.dataLoad leggiArgomentiDellaSezione:tag];
self.table = [[SezioniTableController alloc] initWithStyle:UITableViewStylePlain andArray:self.arrayTesti];
self.tableArgumentView.delegate = self.table;
self.tableArgumentView.dataSource = self.table;
[self.tableArgumentView reloadData];
The viewcontroller is for an iPad application, the table works perfectly but if I try to search something it doesn't work!
Can you help me?
If you assign
self.arraySezioni = array;
self.arrayFiltrato = array;
then both self.arraySezioni and self.arrayFiltrato are pointers to the same array. Which means that if you empty one array with
[self.arrayFiltrato removeAllObjects];
then the other array self.arraySezioni (and the original array) are also empty.
So at least self.arrayFiltrato should be a copy of the original array:
self.arrayFiltrato = [array copy];

Resources