Searchbar is not working iPhone Xcode 4.2

人盡茶涼 提交于 2020-01-05 07:28:30

问题


I am using this code for search bar, but it is not showing any results.

in viewdidload method:

 filteredListitems = [[NSMutableArray alloc] initWithArray:listVehicles];

searchbar methods:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

if ([searchText length] == 0) {
    [filteredListitems removeAllObjects];
    [filteredListitems addObjectsFromArray:listVehicles];
}else {

    [filteredListitems removeAllObjects];
    for (NSString * string in listVehicles) {
        NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (r.location != NSNotFound) {
            [filteredListitems addObject:string];
        }
    }
}    
[listTable reloadData];}

-(void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {

[searchBar resignFirstResponder];
}

and code for each cell is:

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

static NSString *CellIdentifier = @"vlCell";

VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    NSLog(@"Cell Created");

    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil];

    for (id currentObject in nibObjects) {

        if ([currentObject isKindOfClass:[VehicleListCell class]]) {

            cell = (VehicleListCell *)currentObject;
        }
    }

    UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
    pressRecongnizer.minimumPressDuration = 0.5f;
    [cell addGestureRecognizer:pressRecongnizer];
    [pressRecongnizer release];

}

cell.textLabel.font = [UIFont systemFontOfSize:10];

    [[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]];
    [[cell direction] setImage:[UIImage imageNamed:@"south.png"]];
    NSString *cellValue = [filteredListitems objectAtIndex:indexPath.row];
    cell.licPlate.text = cellValue;

return cell;

}

I have tried search bar with display controller, it was working fine but the issue was that it was showing its own table view for the filtered search results, while I am using custom cell to show different columns in table like this:

I want the same view as above while searching or after search is done

while after search I get this view

See the difference between the tableviews, as in search headers get disappear, so someone suggested me to use only seachbar without display controllers.

Please guide me so that I would resolve this issue


回答1:


You should use a Search Display Controller.

The key is that it will call your table view data source & delegate methods, but pass you its table view as the first parameter.

For example (if you store a reference to your table view in an instance variable named yourTableView):

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == yourTableView) {
        return [listVehicles count];
    } else { // handle search results table view
        return [filteredListItems count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellValue = nil;

    if (tableView == yourTableView) {
        cellValue = [listVehicles objectAtIndex:indexPath.row];
    } else { // handle search results table view
        cellValue = [filteredListItems objectAtIndex:indexPath.row];
    }

    static NSString *CellIdentifier = @"vlCell";

    VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

    NSLog(@"Cell Created");

    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil];

      for (id currentObject in nibObjects) {
          if ([currentObject isKindOfClass:[VehicleListCell class]]) {
              cell = (VehicleListCell *)currentObject;
          }
      }

      UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
      pressRecongnizer.minimumPressDuration = 0.5f;
      [cell addGestureRecognizer:pressRecongnizer];
      [pressRecongnizer release];
    }

    cell.textLabel.font = [UIFont systemFontOfSize:10];

    [[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]];
    [[cell direction] setImage:[UIImage imageNamed:@"south.png"]];

    cell.licPlate.text = cellValue;

    return cell;
}



回答2:


I have used search View Controller in my application and implementing this delegate method

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
if(self.searchDisplayController.searchBar.text.length>0) {
    self.isSearching=YES;

    NSString *strSearchText = self.searchDisplayController.searchBar.text;
    NSMutableArray *ar=[NSMutableArray array];
    // correctly working ! Thanx for watching video !
    for (int i=0; i<self.arOriginal.count; i++) {
        NSString *strData = [self.arOriginal objectAtIndex:i];
        NSRange rng = [strData rangeOfString:strSearchText options:NSCaseInsensitivePredicateOption];
        if(rng.length > 0){
            [ar addObject:strData];
        }
    }
    self.arFiltered=[NSArray arrayWithArray:ar];
} else {
    self.isSearching=NO;
}
return YES;
}


来源:https://stackoverflow.com/questions/8922456/searchbar-is-not-working-iphone-xcode-4-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!