Objective C UISearchBar - objective-c

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.

Related

How to detecte no results in GMSAutocompleteViewController

I'm using GMSAutocompleteViewController to get places in my app which make it easier to the user and neat to my server data. and this is my code below.
My question is how can I detect of the auto-complete returned no result so I can handle this ?
#pragma mark - GMSAutocompleteViewControllerDelegate
// Handle the user's selection.
- (void)viewController:(GMSAutocompleteViewController *)viewController didAutocompleteWithPlace:(GMSPlace *)place {
[self dismissViewControllerAnimated:YES completion:nil];
// Do something with the selected place.
[_institutionTextField setText:place.name];
[_addressTextField setText:[[place.addressComponents objectAtIndex:0] name]];
[errorStatus replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:YES]];
[errorStatus replaceObjectAtIndex:1 withObject:[NSNumber numberWithBool:YES]];
[experinceDictionary setObject:#{#"name":place.name,
#"latitude":[NSNumber numberWithFloat:place.coordinate.latitude],
#"longitude":[NSNumber numberWithFloat:place.coordinate.longitude],
#"address":place.formattedAddress,
#"google_place_id":place.placeID}
forKey:#"hospital"];
}
- (void)viewController:(GMSAutocompleteViewController *)viewController didFailAutocompleteWithError:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:nil];
// TODO: handle the error.
NSLog(#"Error: %#", [error description]);
}
// User canceled the operation.
- (void)wasCancelled:(GMSAutocompleteViewController *)viewController {
[self dismissViewControllerAnimated:YES completion:nil];
}
// Turn the network activity indicator on and off again.
- (void)didRequestAutocompletePredictions:(GMSAutocompleteViewController *)viewController {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)didUpdateAutocompletePredictions:(GMSAutocompleteViewController *)viewController {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == _institutionTextField) {
GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
acController.delegate = self;
[self presentViewController:acController animated:YES completion:nil];
}
}

UISearchDisplayController result table position issue in iOS7

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];
}

Passing a string to another view

I have a UITextField in my first view that accepts search criteria however my search box is in my second view. The idea is to pass the textfield data that the user enters into the search box that filters a table view in the second view controller. I have tried setting the secondviewcontroller.searchText = self.search.text however it doesn't seem to be updating the search box in the second view controller with the new data. My code is as follows:
First View.m
- (IBAction)search:(id)sender
{
PlaceList *placelist = [[PlaceList alloc] initWithNibName:#"PlaceList" bundle:nil];
NSLog(#"%#", search.text);
placelist.searchBar.text = search.text;
[self.navigationController pushViewController:placelist animated:YES];
[placelist release];
}
PlaceList.m
#pragma mark UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar’s cancel button while in edit mode
useSearchData = YES;
self.searchBar.showsCancelButton = YES;
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
// flush the previous search content
[tableData removeAllObjects];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
self.searchBar.showsCancelButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if([searchText isEqualToString:#""] && [__searchBar.text isEqualToString:#""]){ //if nothing is in the search bar show normal table
useSearchData = NO;
[self.tableView reloadData];
[self.searchBar resignFirstResponder];
return;
}
else
{
searchText = __searchBar.text;
useSearchData=YES;
NSPredicate * p = [NSPredicate predicateWithFormat:#"name contains[cd] %#",searchText]; //comparing stored locations to searchText
self.searchResults = [CoreDataBasicService fetchResultsForEnity:#"Place" WithPredicate:p andSortDiscriptor:#"name" Ascending:YES];
[self.tableView reloadData];
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
useSearchData = NO;
[self.searchResults removeAllObjects];
[self.tableView reloadData];
[self.searchBar resignFirstResponder];
self.searchBar.text = #"";
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)_searchBar
{
[searchBar resignFirstResponder];
}
When the search IBAction is pressed I want to push to the placeList view and update the search with the text the user entered in the textfield on the first view controller.
Thanks in advance.
Try initializing PlaceList(VC) with the search Text, saving it to an instance variable. Then, in your PlaceList(VC)'s ViewDidLoad Method, set the text there

How to reload UITableView after the UISearchBar ends editing

I created a UISearchBar. It is working but not the way I want. If I enter any first letter on the UISearchbar and then I click on the SearchButton, it doesn't work but when I push the next controller and I come back, then I see my search result in TableView. The first time my TableView does not refresh.
Here is my custom cell class and my controller class
#synthesize myTableView;
#synthesize tabledata;
#pragma mark -
#pragma mark Initialization
#pragma mark -
#pragma mark View lifecycle
-(void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
app = (JourneyAppDelegate *)[[UIApplication sharedApplication]delegate];
sBar =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
sBar.delegate=self;
[self.view addSubview:sBar];
searcheddata =[[NSMutableArray alloc]init];
NSLog(#"*************:%&",list);
list=[[NSMutableArray alloc]init];
tabledata =[[NSMutableArray alloc]init];
list = [app.journeyList retain];
[tabledata addObjectsFromArray:list];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100.0;
}
#pragma mark -
#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 [tabledata count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"*************:%&",list);
static NSString *CellIdentifier = #"Cell";
TJourneyListCell *cell =(TJourneyListCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[TJourneyListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSLog(#"%#",cell);
}
NewJourney *newjoruneobject = [tabledata objectAtIndex:indexPath.row];
cell.namelbl.text = newjoruneobject.journeyname;
cell.distancelbl.text = newjoruneobject.journeylocation;
cell.infolbl.text = newjoruneobject.journeydescription;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
#pragma mark UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar’s cancel button while in edit mode
[tabledata removeAllObjects];
sBar.showsCancelButton = YES;
[searchBar setShowsCancelButton:YES animated:YES];
sBar.autocorrectionType = UITextAutocorrectionTypeNo;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[tabledata removeAllObjects];// remove all data that belongs to previous search
if([sBar.text length] != 0)//|| searchText==nil)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"journeyname contains[cd] %#", searchBar.text];
[tabledata addObjectsFromArray:[list filteredArrayUsingPredicate: predicate]];
[myTableView reloadData];
return;
}
NSLog(#"Counter:-'%d'",[tabledata count]);
[myTableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{ // if a valid search was entered but the user wanted to cancel, bring back the main list content
[tabledata removeAllObjects];
[tabledata addObjectsFromArray:list];
#try
{
[myTableView reloadData];
}
#catch(NSException *e)
{
}
[sBar resignFirstResponder];
sBar.text = #" ";
}
// called when Search (in our case “Done”) button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
myTableView.allowsSelection = YES;
myTableView.scrollEnabled = YES;
[myTableView reloadData];
}
- (void)dealloc {
[super dealloc];
[mLable1 release];
[myTableView release], myTableView = nil;
[tabledata dealloc];
}
#end
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = NO;
[myTableView reloadData];
}
I was having difficulties with this as well until I got to this post. I looked through your functions and decided to throw the reload data in the function above it works fine now! Hope this helps!
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tableView reloadData];
}
You can use this delegate method to reload table, after search table disappears:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
Swift version
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false //Local variable used to manipulate your cells
self.newsTableView.reloadData()
}

UITableView losing height when i search

Im getting a little trouble here. I have a UISearchBar that searchs for me some content, that are included on UITableView. Perfect.
When my view loads, i set a 110 height to my table view row. Nice. When my view appears, it comes with 110 px. of height. Nice.
But when i click on UISearchBar, my rows loses the height setted.
I dont know what can be.
Any help, thanks!
- (void)viewDidLoad {
[super viewDidLoad];
theTableView.rowHeight = 110;
theTableView.backgroundColor = [UIColor clearColor];
}
And the search code:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
self.theTableView.allowsSelection = NO;
self.theTableView.scrollEnabled = NO;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=#"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// All the actions goes here, like parsing xml.
[self.tableData removeAllObjects];
[theTableView reloadData];
}
Thanks!