0

I am trying to do a search in a UITableview. I have implemented the UISearchDisplayDelegate, UISearchBarDelegate method at the correct way. This is how my cellForRowAtIndexPath looks like.

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

}

And this is my filterContentForSearchText method

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

The strange thing is. In my cellForRowAtIndexPath it returns me the correct data. But the tableview itselfs keeps given me the NO RESULTS label.

Any help with this?

Steaphann
  • 2,797
  • 6
  • 50
  • 109

3 Answers3

0
  1. Use an array to show data in table(showDataArray)
  2. Use an array to store all input values(dataArray)
  3. Use showDataArray to populate your table always
  4. In the searchfield when charecter range changes call a method to filter the data using predicate from dataArray
  5. Save the value to showDataArray
  6. Call for table reloadData
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

Follow this tutorial how-to-add-search-bar-uitableview ..

Just see the method cellForRowAtIndexPath: in which how to set the search array when user searched record from 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;
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • Thank you for your answer! Could you please take a look at this question also ? http://stackoverflow.com/questions/14768859/how-to-acces-the-right-navigation-controller-inside-storyboard/14769261#comment20677346_14769261 – Steaphann Feb 08 '13 at 11:07
0

First add UISearchBar on top of UITabelView documentation

And then Take Two NSMutableArray and add one array to another array in ViewDidLoad method such like,

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

And Write following delegate Method of 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];
}

And Write in 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;
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
iPatel
  • 46,010
  • 16
  • 115
  • 137