搜索栏xcode

时间:2014-08-07 15:16:37

标签: ios json xcode5 searchbar

我有一个适用于IOS的新应用,我正在使用搜索栏来解析JSON和我。解析JSON工作正常,但我在搜索栏中遇到问题,即当我构建我的应用程序并且我想搜索一些被解析的单词时我什么都没看到。下面我把我的部分代码。我认为问题在于谓词:" predicateWithFormat:@" productName如[cd]%@",searchText];"任何人都可以帮助我吗?

tableViewController.m

-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"productName like[cd] %@", searchText];

    searchResults = [productsArray filteredArrayUsingPredicate:predicate];
}



-(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
    shouldReloadTableForSearchString:(NSString *)searchString
{

    [self filterContentForSearchText:searchString
                           scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                  objectAtIndex:[self.searchDisplayController.searchBar
                                                 selectedScopeButtonIndex]]]; 
    return YES;
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == self.searchDisplayController.searchResultsTableView){
       return [searchResults count];  
    } else {
        return [productsArray count]; }
}


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

    static NSString *simpleTableIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    Product *productObject;
    productObject = [productsArray objectAtIndex:indexPath.row];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

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

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"Cell" sender: self];
    }
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"Cell"]) {

        DetailViewController *destViewController = segue.destinationViewController;



        if ([self.searchDisplayController isActive]) {

            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            destViewController.imageView = [searchResults objectAtIndex:indexPath.row];

        } else {

            //indexPath = [self.tableView indexPathForSelectedRow];

            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            Product * object =[productsArray objectAtIndex:indexPath.row];
            [[segue destinationViewController] getProduct: object];
            //destViewController.idLabel = [productsArray objectAtIndex:indexPath.row];
        }
    }
}

3 个答案:

答案 0 :(得分:0)

这个问题最有可能出现在谓词设置中,正如您所怀疑的那样。

我使用类似的东西在联系人列表中搜索匹配项,也许这是一个可行的选择:

[NSPredicate predicateWithBlock:^BOOL(Contact *contact, NSDictionary *bindings){
    return [contact.name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound;
}];

答案 1 :(得分:0)

我解决了这个问题。问题不在谓词中,而是在cellForRowAtIndexPath中:我在第二个if语句中犯了错误,它应该是这样的:

Product *prod = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView) {

        prod=[searchResults objectAtIndex:indexPath.row];
            cell.textLabel.text = prod.ProductName;
        }
        else
        {
            cell.textLabel.text = productObject.productName;
        }
        return cell;

答案 2 :(得分:0)

  -(void)customizeSearchBar{
[self.searchBar setBackgroundImage:[[UIImage alloc]init]];

// Backgroud Color
[self.searchBar setBackgroundColor:[UIColor clearColor]];

// Search Bar Cancel Button Color
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];

[self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(10.0, 0.0)];

// set Search Bar textfield background image
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"search-bar.png"]
                                               forState:UIControlStateNormal];

// set Search Bar texfield corder radius
UITextField *txfSearchField = [self.searchBar valueForKey:@"_searchField"];
txfSearchField.layer.cornerRadius = 14.0f;
txfSearchField.clearButtonMode=UITextFieldViewModeNever;
}
   #pragma mark - Search delegate methods

  -(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
[self.searchBar setShowsCancelButton:YES animated:YES];
//    UITextField *txfSearchField = [self.searchBar valueForKey:@"_searchField"];
//    [txfSearchField setLeftViewMode:UITextFieldViewModeNever];
UIView* view=self.searchBar.subviews[0];
for (UIView *subView in view.subviews) {
    if ([subView isKindOfClass:[UIButton class]]) {
        UIButton *cancelButton = (UIButton*)subView;
        [cancelButton setTitleColor:COLOR_RGB(250, 109, 40) forState:UIControlStateNormal];
        [cancelButton.titleLabel setFont:[UIFont fontWithName:FONT_ROBOTO_REGULAR size:FONT_SIZE_13]];
    }
}
return YES;
  }

  -(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
  [self.searchBar setShowsCancelButton:NO animated:YES];
  return YES;
  }

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:   (NSString   *)searchText
   {
     if (searchText.length>0) {
       NSPredicate *resultPredicate = [NSPredicate  predicateWithFormat:@"department_name contains[c] %@", searchText];
    NSArray *arr = [self.arrData filteredArrayUsingPredicate:resultPredicate];
    self.arrOperation = [NSMutableArray arrayWithArray:arr];

}
else
{
    self.arrOperation = [NSMutableArray arrayWithArray:self.arrData];
}
if(self.arrOperation.count == 0)
{
    self.imgNoDataMessage.hidden=NO;
   // self.imgNoDataMessage.image=IMAGE_NAME(@"instore.png");
}
else
{
    [self hideMessage];

}
[self.tblView reloadData];
}


 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
UITextField *txfSearchField = [searchBar valueForKey:@"_searchField"];
[txfSearchField setLeftViewMode:UITextFieldViewModeAlways];
[searchBar performSelectorOnMainThread:@selector(resignFirstResponder) withObject:nil waitUntilDone:NO];
     }

 -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
self.searchBar.text = @"";
self.arrOperation = [NSMutableArray arrayWithArray:self.arrData];
UITextField *txfSearchField = [searchBar valueForKey:@"_searchField"];
[txfSearchField setLeftViewMode:UITextFieldViewModeAlways];
if(self.arrOperation.count == 0)
{
    self.imgNoDataMessage.hidden=NO;
   // self.imgNoDataMessage.image=IMAGE_NAME(@"instore.png");
}
else
{
    [self hideMessage];
}
[self.tblView reloadData];
[searchBar performSelectorOnMainThread:@selector(resignFirstResponder) withObject:nil waitUntilDone:NO];
}