UIsearch bar not returning data to table

与世无争的帅哥 提交于 2019-12-23 05:31:14

问题


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.


回答1:


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:.




回答2:


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




回答3:


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



回答4:


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



回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/11257681/uisearch-bar-not-returning-data-to-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!