0

我正在尝试在 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 标签。

有什么帮助吗?

4

3 回答 3

0

首先在 UITabelView 文档之上添加 UISearchBar

然后取两个NSMutableArray并将一个数组添加到另一个数组ViewDidLoad,例如,

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;
}
于 2013-02-08T11:28:03.193 回答
0

按照本教程如何添加搜索栏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;
}
于 2013-02-08T11:00:34.920 回答
0
  1. 使用数组显示表中的数据(showDataArray)
  2. 使用数组存储所有输入值(dataArray)
  3. 始终使用 showDataArray 填充您的表格
  4. 在搜索字段中,当字符范围更改时,调用一个方法来使用 dataArray 中的谓词过滤数据
  5. 将值保存到 showDataArray
  6. 调用表 reloadData
于 2013-02-08T10:52:03.090 回答