BeaTX
BeaTX

Reputation: 21

UIsearch bar not returning data to table

Edited code

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


if (isFiltered) {
   int rowCount=indexPath.row;
   Aves *filtrada=[filteredTableData objectAtIndex:rowCount];
   cell.textLabel.text=filtrada.name;
   NSLog(@"mostrando: ");
    }else {
        int rowCounter=indexPath.row;
        Aves *author=[theauthors objectAtIndex:rowCounter];
        cell.textLabel.text=author.name;
    }
NSLog(@"mostrando: ");
return cell;

}

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
    if(text.length == 0)
    {
        isFiltered = FALSE;
    }
    else
    {
        isFiltered = true;
        int i;
        [filteredTableData removeAllObjects];
        for(i=0;[theauthors count]>i;i++)
        {
          Aves *name=[theauthors objectAtIndex:i];
            //NSLog(name.name);
            NSRange nameRange = [[name.name lowercaseString] rangeOfString:[text lowercaseString]];
            if(nameRange.length>0)
            {
                [filteredTableData addObject:name];
                NSLog(name.name);
            }
        }
        [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    }
}

Edit: After working on it a while I solved some problems.Just updated my code, the problem is the repaint of the tableView, every thing else go ok. Check it and give any ideas you have plz ^^

Thx again for your time.

Upvotes: 0

Views: 263

Answers (6)

ecasper
ecasper

Reputation: 509

You probably want to replace

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

cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                            reuseIdentifier:CellIdentifier];

with in your cellForRowAtIndexPath

UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                            reuseIdentifier:CellIdentifier];

The reason because you're not getting use of the dequeued cell anyway.

Upvotes: 0

BeaTX
BeaTX

Reputation: 21

finally fixed it. Here is my working code, thx you all =)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

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

    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                reuseIdentifier:CellIdentifier];

    if (isFiltered==TRUE) {
        int rowCount=indexPath.row;
        //for (rowCount=0; rowCount<[filteredTableData count]; rowCount++) {
        Aves *filtrada=[filteredTableData objectAtIndex:rowCount];
        cell.textLabel.text=filtrada.name;
        //}

    }else if(isFiltered==FALSE) 
    {
        int rowCounter=indexPath.row;
        Aves *author=[theauthors objectAtIndex:rowCounter];
        cell.textLabel.text=author.name;
    }
    return cell; 
}

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text {

  [filteredTableData removeAllObjects];

filteredTableData=[[NSMutableArray alloc]init ];

if(text.length == 0)
{
    isFiltered = FALSE;
}
else
{
    isFiltered = TRUE;
    int i;

    for(i=0;[theauthors count]>i;i++)
    {
        Aves * filtrado=[[Aves alloc]init];
        filtrado=[theauthors objectAtIndex:i];
        //NSLog(filtrado.name);
        NSRange nameRange = [[filtrado.name lowercaseString] rangeOfString:[text lowercaseString]];
        if(nameRange.length>0)
        {
            [filteredTableData addObject:filtrado];

        }
    }
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil

waitUntilDone:NO]; } }

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

use this:

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
   {
  //If the requesting table view is the search display controller's table view, return the count of the filtered list, otherwise return the count of the main list.

     if (isFiltered)
     {
       return [filteredTableData count];
     }
     else
     {
       return [theauthors count];
     }
  }

Replace method:

   -(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
   {
     if(text.length == 0)
     {
       isFiltered = FALSE;
     }
     else
     {
      isFiltered = true;
      int i=0;
      [filteredTableData removeAllObjects];
      filteredTableData = [[NSMutableArray alloc] init];
      //for (Aves* name in theauthors)
      for(i=0;[theauthors count]>i;i++)
      {
        Aves *name=[theauthors objectAtIndex:i];
        NSRange nameRange = [[name.foodName lowercaseString] rangeOfString:[text lowercaseString]];

        if(nameRange.length>0)
        {
            [filteredTableData addObject:name];
            [self.tableView reloadData];
        }
       }
     }
   }

Upvotes: 0

Eren Beşel
Eren Beşel

Reputation: 1057

Change the code piece of

else
{
    isFiltered = true;
    int i=0;
    [filteredTableData removeAllObjects];
    filteredTableData = [[NSMutableArray alloc] init];
    //for (Aves* name in theauthors)
    for(i=0;[theauthors count]>i;i++)
    {
        Aves *name=[theauthors objectAtIndex:i];
        NSRange nameRange = [name.foodName rangeOfString:text ];

        if(nameRange.location ==0)
        {
            [filteredTableData addObject:name];
        }
        [self.tableView reloadData];
    }
}

to

else
{
    isFiltered = true;
    int i=0;
    [filteredTableData removeAllObjects];
    //filteredTableData = [[NSMutableArray alloc] init]; not needed
    //for (Aves* name in theauthors)
    for(i=0;[theauthors count]>i;i++)
    {
        Aves *name=[theauthors objectAtIndex:i];
        NSRange nameRange = [name.foodName rangeOfString:text ];

        if(nameRange.location != NSNotFound)
        {
            [filteredTableData addObject:name];
        }
    }
    [self.tableView reloadData];
}

Upvotes: 0

Rendy
Rendy

Reputation: 5698

does your app hits this code if(nameRange.location ==0) ?

Upvotes: 0

zpasternack
zpasternack

Reputation: 17898

I assume you're using prototype cells? I just had a similar problem in one of my projects.

When search results are displayed and tableView:cellForRowAtIndexPath: is called, the table view passed in the the table belonging to the search results controller, not your main table view. Problem with that is, the search results table view doesn't know anything about your table's prototype cells, so dequeueReusableCellWithIdentifier: returns nil. But just alloc/init'ing a UITableCellView won't give you one of your prototype cells, so whatever UI you laid out in your storyboard isn't there.

The fix is easy: in tableView:cellForRowAtIndexPath:, don't call dequeueReusableCellWithIdentifier: on the tableview passed in; just call it on your main table view. So basically, just change this:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) 
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

to this:

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

There's no need for the nil check; Apple's Storyboards Release Notes says:

The dequeueReusableCellWithIdentifier: method is guaranteed to return a cell (provided that you have defined a cell with the given identifier). Thus there is no need to use the “check the return value of the method” pattern as was the case in the previous typical implementation of tableView:cellForRowAtIndexPath:.

Upvotes: 1

Related Questions