【问题标题】:Searchbar is not working iPhone Xcode 4.2搜索栏不工作 iPhone Xcode 4.2
【发布时间】:2012-01-19 07:29:03
【问题描述】:

我正在将此代码用于搜索栏,但它没有显示任何结果。

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

}

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

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

搜索后我得到了这个视图

查看表格视图之间的区别,因为搜索标题会消失,所以有人建议我只使用没有显示控制器的搜索栏。

请指导我,以便我解决此问题

【问题讨论】:

  • NSLog 你的数组过滤列表项在 if 语句中。
  • 选中,搜索按钮不起作用,filteredListItems 数组显示了 listVehicles 的所有项目
  • 如何在视图中添加搜索栏?
  • 从对象库中拖放
  • 是搜索栏还是 searchViewController ?你有没有提供参考?

标签: iphone objective-c ios xcode4.2


【解决方案1】:

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

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

例如(如果您将对表视图的引用存储在名为 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;
}

【讨论】:

  • hey gerry3 thanx dude.... 得到我想要的搜索表视图,thanx 提供了这些有用的信息,因为表视图和搜索栏控制器主要用于应用程序中。我还有一个问题,您在搜索中为我自己的 tableview 提供的代码给出了与 tableview 相同的结果,但是我使用 A、B、C 和 D 的标题我如何在搜索中也显示它们表格视图
  • 要在搜索结果表上显示标题,您可以尝试添加一个带有标签的视图作为搜索结果表的表头视图。
  • 怎么做...还有一件事,那就是当我搜索时,搜索显示表在有结果的单元格中显示背景图像,而其他单元格没有background ,它们是默认的。
  • 设置标题视图:self.tableView.tableHeaderView = yourHeaderView;。您正在返回结果表视图使用的单元格,所以我认为它们会有您的单元格背景。我经常实现tableView:willDisplayCell:forRowAtIndexPath:并在那里设置单元格背景颜色。
  • 谢谢 gerry3,你帮了我很多兄弟 :)
【解决方案2】:

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

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

【讨论】:

  • thanx hiren443,但是关于搜索结果的表格视图问题,我该如何解决它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多