2016-09-26 80 views
2

我是新来的ios和我面临的问题有关UISearchbar。我做了这样的代码如何显示UISearchbar栏只在tableview不在UINavigationbar目标c

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

但它显示我的搜索栏导航栏

enter image description here

上,但我需要证明它的UITableView的本身。像这样在图像 enter image description here

在此先感谢

回答

0

我使用代码从4可变数组搜索...

- (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 *[email protected]"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; 
     } 



    } 
相关问题