2
votes

I have the following UITableViewController + UISearchBar setup

@interface FeedTableView : UITableViewController <UISearchBarDelegate,UISearchDisplayDelegate>
{
    NSMutableArray *ArrayDatiOriginali;
    NSMutableArray *searchData;

    UISearchBar *searchBar;
    UISearchDisplayController *searchDisplayController;    
}

Below the load method

- (void)viewDidLoad
{
    [super viewDidLoad];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate=self;

    self.tableView.tableHeaderView = searchBar;  tableView.
}

To select data/rows when the UISeach appear i use

if(tableView==self.searchDisplayController.searchResultsTableView)
{
    NSLog(@"Riga corrente: %i",indexPath.row);
    appo=[searchData objectAtIndex:indexPath.row];
}

and it works ok for filtering table view.

But if:

  • Search view is active (showing filtered result)
  • I click on a row of the filtered result

Then

  • didSelectRowAtIndexPath is fired but
  • in the cellForRowAtIndexPath method the expression

    if(tableView==self.searchDisplayController.searchResultsTableView) return FALSE

also if UISearchView is active

Any ideas?

2
Why would you return FALSE from cellForRowAtIndexPath? I don't really follow what the problem is - the selection works but after selection the display doesn't? - Wain
I want to update cell image after user select a row in the UISearchResultController. For this the didSelectRowAtIndexPath works ok, but I need to reload the UISearch cell/table to update the image - user3197643
Fine. You should be updating your data model as a result of the selection and then reloading the table. This reloads the cell which takes the new information from the data model. - Wain
@user3197643 Did you find the answer? I have same problem. - Hadi Sharghi

2 Answers

6
votes

Make Two Arrays.

dataArray & filteredDataArray & Make a BOOL isFiltered.

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
    isFiltered = FALSE;
}
else
{
    isFiltered = true;
}
[self.tableView reloadData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
isFiltered = FALSE;
[searchBar resignFirstResponder];
[self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(isFiltered)
{
    <#YOUR_MODEL#>= [filteredDataArray objectAtIndex:indexPath.row];
}
else
{
    <#YOUR_MODEL#>= [dataArray objectAtIndex:indexPath.row];
}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rowCount;
if(isFiltered)
    rowCount = [filteredDataArray count];
else
    rowCount = [dataArray count];
return rowCount;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// [tableView deselectRowAtIndexPath:indexPath animated:YES];
if(isFiltered)
{
    <#YOUR_MODEL#>= [filteredDataArray objectAtIndex:indexPath.row];
}
else
{
    <#YOUR_MODEL#>= [dataArray objectAtIndex:indexPath.row];
}
//Pass it to any class or do what ever.
}
0
votes

To add to the previous answer, you also need to add:

searchBar.delegate = self;

The searchBar will then be able to call

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