【问题标题】:Searching a UITableview搜索 UITableview
【发布时间】:2013-02-08 10:45:46
【问题描述】:

我正在尝试在 UITableview 中进行搜索。我已经以正确的方式实现了 UISearchDisplayDelegate、UISearchBarDelegate 方法。这就是我的 cellForRowAtIndexPath 的样子。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if ( cell == nil ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    if (tableView == self.searchDisplayController.searchResultsTableView){
        Contact *contact = [self.filteredListContent objectAtIndex:indexPath.row];

        NSString *text = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName];
        NSLog(@"CellForRowAtIndexPath contact text is %@",text);
        cell.textLabel.text = text;


        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];


    }else{
    NSString *alphabet = [firstIndex objectAtIndex:[indexPath section]];

    //---get all states beginning with the letter---
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@",alphabet];
    NSArray *contacts = [listContent filteredArrayUsingPredicate:predicate];
    Contact *contact = [contacts objectAtIndex:indexPath.row];

    NSString *text = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName];
    cell.textLabel.text = text;


       [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }

    return cell;

}

这是我的 filterContentForSearchText 方法

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    [self.filteredListContent removeAllObjects]; // First clear the filtered array.

    for (Contact *contact in listContent)
    {
        NSString *searchString = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName];
        NSRange range = [searchString rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (range.location != NSNotFound) {
            [self.filteredListContent addObject:contact];
            [self.searchDisplayController.searchResultsTableView reloadData];
        }
    }
}

奇怪的是。在我的 cellForRowAtIndexPath 中,它返回了正确的数据。但是 tableview 本身一直给我 NO RESULTS 标签。

有什么帮助吗?

【问题讨论】:

标签: iphone ios objective-c uitableview uisearchbar


【解决方案1】:

按照本教程how-to-add-search-bar-uitableview ..

请看cellForRowAtIndexPath:方法,当用户从UISearchBar搜索记录时如何设置搜索数组...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"RecipeCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    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 = [recipes objectAtIndex:indexPath.row];
    }
    
    return cell;
}

【讨论】:

【解决方案2】:
  1. 使用数组显示表中的数据(showDataArray)
  2. 使用数组存储所有输入值(dataArray)
  3. 始终使用 showDataArray 填充您的表格
  4. 在搜索字段中,当字符范围发生变化时,调用方法以使用 dataArray 中的谓词过滤数据
  5. 将值保存到 showDataArray
  6. 调用表 reloadData

【讨论】:

    【解决方案3】:

    先添加UISearchBar on top of UITabelView documentation

    然后在ViewDidLoad方法中取两个NSMutableArray将一个数组添加到另一个数组

    self.listOfTemArray = [[NSMutableArray alloc] init]; // array no - 1
    self.ItemOfMainArray = [[NSMutableArray alloc] initWithObjects:@"YorArrayList", nil]; // array no - 2 
    
    [self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray]; // add 2array to 1 array
    

    并编写如下 UISearchBar 的委托方法

    - (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
    {
            NSString *name = @"";
            NSString *firstLetter = @"";
    
        if (self.listOfTemArray.count > 0)
             [self.listOfTemArray removeAllObjects];
        
            if ([searchText length] > 0)
            {
                    for (int i = 0; i < [self.ItemOfMainArray count] ; i = i+1)
                    {
                            name = [self.ItemOfMainArray objectAtIndex:i];
                            
                            if (name.length >= searchText.length)
                            {
                                    firstLetter = [name substringWithRange:NSMakeRange(0, [searchText length])];
                                    //NSLog(@"%@",firstLetter);
                                    
                                    if( [firstLetter caseInsensitiveCompare:searchText] == NSOrderedSame )
                                    {
                                        // strings are equal except for possibly case
                                        [self.listOfTemArray addObject: [self.ItemOfMainArray objectAtIndex:i]];
                                        NSLog(@"=========> %@",self.listOfTemArray);
                                    }
                             }
                     }
             }
             else
             {
                 [self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray ];
             }
            
            [self.tblView reloadData];
    }
    

    然后写信cellForRowAtIndexPath

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Foobar"];
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foobar"];
            
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
            cell.textLabel.textColor = [UIColor blackColor];
        }
        
            cell.textLabel.text = [self.listOfTemArray objectAtIndex: indexPath.row];
      
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      相关资源
      最近更新 更多