【问题标题】:UISearchcontroller is not working correctlyUISearchcontroller 无法正常工作
【发布时间】:2023-03-31 17:28:01
【问题描述】:

我已经实现了如下的搜索功能。我能够根据搜索字符串检索行,但它们是空的,当我单击空的搜索结果单元格时,我能够正确导航每个搜索结果。我正在使用故事板配置的单元格来显示表格。我的意思是使用标签来填充数据和图像。

- (void)viewDidLoad
{

    sqlDatabase = [SQLiteDatabase getDatabaseInstance];           
    filteredItemist=[[NSMutableArray alloc] initWithArray:[sqlDatabase fetchCropListBySoilName:soilName]];
    [super viewDidLoad];
}




- (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] ;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    // Configure the cell...

     Crop * crop = [filteredItemist objectAtIndex:[indexPath row]];


    NSString* str = [NSString stringWithFormat:@"%@.jpg",crop.cropName.lowercaseString];

    UIImageView *cropImageView = (UIImageView *)[cell viewWithTag:100];
    cropImageView.image = [UIImage imageNamed:str];

    UILabel *cropNameLabel = (UILabel *)[cell viewWithTag:70];
    cropNameLabel.text = [crop cropName];

    return cell;

}


- (void)viewWillAppear:(BOOL)animated
{
   self.navigationItem.title = @"Item List";
    filteredItemist = [[NSMutableArray alloc]initWithArray:[sqlDatabase fetchCropListBySoilName:soilName]];
    [self.tableView reloadData];
    self.navigationController.toolbarHidden = YES;
    [super viewWillAppear:animated];
}


- (void)filterContentForSearchText:(NSString*)searchText
{
    if([searchText length] == 0)
    {
        isFiltered = FALSE;
        [filteredItemist removeAllObjects];
        [filteredItemist addObjectsFromArray:[sqlDatabase fetchCropListBySoilName:soilName]];

    }
    else{
        isFiltered = TRUE;
        [filteredItemist removeAllObjects];
        for(Crop *i in [sqlDatabase fetchCropListBySoilName:soilName]){
            NSRange stringRange = [[i cropName]rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if(stringRange.location !=NSNotFound){
                [filteredItemist addObject:i];
            }
        }

    }
    [self.tableView reloadData];

}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString];

    return YES;
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
}

编辑:

将代码更改为 self.tableview 后:

【问题讨论】:

  • 记录您的 Crop 对象。
  • 这些是 NSInteger cropId 中的属性; NSString* 作物名称; NSInteger 农场 ID; NSInteger Soil_id;并且在搜索结果中,该对象具有数据,但不知何故无法在表格重新加载时从情节提要中填充标签。

标签: ios objective-c ios5 uitableview


【解决方案1】:

而不是这个:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

使用这个:

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

搜索结果显示在单独的表格视图中。因此,如果您使用不带 self 的 tableView,您可能指的是搜索结果表视图。

【讨论】:

  • 非常感谢。我现在正在获取数据。但是单元格大小不是我在初始视图中得到的,也不是我在故事板中定义的,它的大小要小得多,因此它将我的单个单元格拆分为结果表中的两个单元格。我已将图像添加为我的编辑邮政 。请检查。谢谢
  • 编辑您的 heightForRowAtIndexPath 方法以反映高度变化。
猜你喜欢
  • 2016-12-01
  • 1970-01-01
  • 2016-09-01
  • 2012-07-11
  • 2018-04-08
  • 2017-04-20
  • 2018-10-02
  • 2016-09-04
  • 2010-10-06
相关资源
最近更新 更多