Muju Muju - 9 days ago 5
Objective-C Question

How to show UISearchbar bar only in tableview not on UINavigationbar in objective c

I am new in ios and I am facing problem regarding to UISearchbar. I have done code like this

.h
IBOutlet UITableView *table;

NSArray *recipes;
NSArray *searchResults;

.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

recipes = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];




}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];

} else {
return [recipes count];

}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
}

return cell;
}


- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];

searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];

return YES;
}


But it shows me search bar on navigation bar

enter image description here

But I need to show it on the UItableView itself. Like this in Image
enter image description here

Thanks in advance

Answer

I have use code to search from 4 mutable array...

  - (void)viewDidLoad {
            [super viewDidLoad];
            [searchtxt addTarget:self action:@selector(textFieldDidChangeClose:) forControlEvents:UIControlEventEditingChanged];

            searchtxt.delegate=self;
        }


        #pragma mark - Seach Button...

        -(void)textFieldDidChangeClose:(UITextField *)textField
        {
            searchTextString=textField.text;
            [self updateSearchArray:searchTextString];
        }
        -(void)updateSearchArray:(NSString *)searchText
        {
            if(searchText.length==0)
            {
                isFilter=NO;
                [table reloadData];
            }
            else{

                isFilter=YES;
                searchArray=[[NSMutableArray alloc]init];
                searchArrayId=[[NSMutableArray alloc] init];
                searchArraystatus=[[NSMutableArray alloc] init];
                searchArraydescription=[[NSMutableArray alloc] init];
                for(NSString *string in idarray){

                    NSRange stringRange=[string rangeOfString:searchText options:NSCaseInsensitiveSearch];
                    if(stringRange.location !=NSNotFound){
                        [searchArray addObject:string];

                        NSInteger index = [idarray indexOfObject:string];
                        [searchArraystatus addObject:[shortnamearray objectAtIndex:index]];
                        [searchArraydescription addObject:[descarray objectAtIndex:index]];
                        [searchArrayId addObject:[complaintidArray objectAtIndex:index]];
                    }
                }

                [table reloadData];
            }
        }

        #pragma mark - TableView Delegate

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
            if(isFilter)
            {
                return [searchArray count];
            }
            else
            {
                return [idarray count];
            }
        }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {


                static NSString *STI=@"STI";
                IBTClosedComplaintCell *cell = (IBTClosedComplaintCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
                if (cell == nil)
                {
                    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"IBTClosedComplaintCell" owner:self options:nil];
                    cell = [nib objectAtIndex:0];
                    cell.accessoryType=UITableViewCellAccessoryNone;
                }

            if(isFilter)
            {
                cell.Complaintnamelbl.text=[NSString stringWithFormat:@"%@",[searchArray objectAtIndex:indexPath.row]];
                cell.statuslbl.text = [NSString stringWithFormat:@"%@",[searchArraystatus objectAtIndex:indexPath.row]];
                cell.descriptiolbl.text=[NSString stringWithFormat:@"%@",[searchArraydescription objectAtIndex:indexPath.row]];
                cell.complaintidlbl.text = [NSString stringWithFormat:@"%@",[searchArrayId objectAtIndex:indexPath.row]];
            }
            else
            {
                cell.Complaintnamelbl.text=[idarray objectAtIndex:indexPath.row];
                cell.statuslbl.text=[shortnamearray objectAtIndex:indexPath.row];
                cell.descriptiolbl.text=[descarray objectAtIndex:indexPath.row];
                cell.complaintidlbl.text=[NSString stringWithFormat:@"%@",[complaintidArray objectAtIndex:indexPath.row]];
            }


                return cell;


        }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
            IBTClosedNextScreen *next =[[IBTClosedNextScreen alloc] initWithNibName:@"IBTClosedNextScreen" bundle:nil];
            [self.navigationController pushViewController:next animated:YES];

        if(isFilter)
        {
            str1=[NSString stringWithFormat:@"%@",[searchArrayId objectAtIndex:indexPath.row]];
            NSLog(@"String =%@",str1);
            next.str =str1;
        }
        else
        {
            str1=[NSString stringWithFormat:@"%@",[self.complaintidArray objectAtIndex:indexPath.row]];
            NSLog(@"String =%@",str1);
            next.str =str1;
        }



    }