搜索栏无法使用iPhone Xcode 4.2

时间:2012-01-19 07:29:03

标签: iphone objective-c ios xcode4.2

我在搜索栏中使用此代码,但未显示任何结果。

在viewdidload方法中

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

搜索栏方法:

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

每个单元格的代码是:

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

}

我已经尝试使用显示控制器的搜索栏,它工作正常,但问题是它显示了自己的表格视图,用于过滤的搜索结果,而我使用自定义单元格在表格中显示不同的列,如下所示: / p>

enter image description here

我想在搜索时或搜索完成后获得与上面相同的视图

在搜索之后我得到了这个视图

enter image description here

查看tableviews之间的差异,因为在搜索标题中消失了,所以有人建议我只使用没有显示控制器的seachbar。

请指导我,以便我解决这个问题

2 个答案:

答案 0 :(得分:2)

您应该使用搜索显示控制器。

关键是它会调用您的表格视图数据源&委托方法,但将其表视图作为第一个参数传递给你。

例如(如果在名为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;
}

答案 1 :(得分:0)

我在我的应用程序中使用了搜索视图控制器并实现了这种委托方法

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