Add mainTableView to represent self.tableView instead of searchResultsTableView - objective-c

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

Related

UISearchDisplay in existing UITableView

So I have a UITableView in my view controller (as well as many different things) and I wanted to add a search option, so UISearchBar will be probably the best way to do it.
Is it possible to add a search function in my existing UITableView or do I have to rewrite all my view controller?
Implement searchbar delegate method
UISearchBarDelegate
SearchDisplayController Apple Sample code iOS9
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
// called only once
return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[searchBar setPlaceholder:#"Search your languages"];
// called twice every time
[searchBar setShowsCancelButton:NO animated:YES];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
// called only once
[searchBar setShowsCancelButton:NO animated:YES];
return YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
// called only once
[searchBar setPlaceholder:#"Select languages"];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length > 0) {
//write your searching code and reload based on search result
//[self searchproduct:searchText];
}
else
{
//arrayLanguage=[searchResultArray mutableCopy];
//reload with original data array when no searching
}
if (searchText.length == 0) {
//when no search reload with original data array
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[_searchBar resignFirstResponder];
}
Thanks

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

Adding UITapGestureRecognizer in UITableViewCell with delete capabilities

I created a custom cell and upon click, it should display a popup.
However, when this is works successfully, it seems to create an issue where I am not able to click the delete button.
It seems that my UITapRecognizer supercedes my method to delete.(Means the popup is displayed instead when I click the delete button )
Any idea how to solve this?
Below is my code to handle tap in the cell (OfficeCell.m)
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openOfficePopover)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[self setUserInteractionEnabled:YES];
[self addGestureRecognizer:tapGestureRecognizer];
self.textLabel.font = [UIFont boldSystemFontOfSize:15];
self.textLabel.textColor = mRgb(0x3a, 0x6c, 0x99);
}
return self;
}
Below is my code to handle the delete in the ViewController :
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
NSInteger section = [indexPath section];
if (section ==1 )
{
return YES;
}
return NO;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source
NSInteger section = [indexPath section];
if(section == 1)
{
[_sectionOffice removeObjectAtIndex:indexPath.row];
}
[self.formView reloadData];
}
}
Just realize that the fix should be really simple :
changing 2 lines of code solve the issue :
From :
[self setUserInteractionEnabled:YES];
[self addGestureRecognizer:tapGestureRecognizer];
To :
[self.contentView setUserInteractionEnabled:YES];
[self.contentView addGestureRecognizer:tapGestureRecognizer];

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()
}

Objective C UISearchBar

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.