PrepareForSegue after UISearchDisplayController

EDIT:

I just found: How to go from the result of a UISearchBarDisplayController to detailViewController

which I will look at!


I combine the “search bar and search display controller” with the underlying data obtained using the ResultsController using a storyboard. That is, I have to distinguish between:

if (self.tableView == self.searchDisplayController.searchResultsTableView) {
    ...

and the case when I just cited the results obtained from the data warehouse.

However, I am unable to get the correct line (pointer path) in the following case:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"StoreDetails"]) {
        UINavigationController *navigationController = segue.destinationViewController;
        StoreDetailsTableViewController *controller = (StoreDetailsTableViewController *)navigationController.topViewController;
        controller.managedObjectContext = self.managedObjectContext;

        Store *storeToDetail = nil;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];  // This gives wrong row when having searched using searchDisplayController
        NSLog(@"Row: %i", indexPath.row);                                                   // row is always 0
        if (self.tableView == self.searchDisplayController.searchResultsTableView) {
            storeToDetail = [self.filteredListContent objectAtIndex:indexPath.row];
        } else {
            storeToDetail = [self.fetchedResultsController objectAtIndexPath:indexPath];
        }

         controller.storeToDetail = storeToDetail;
    }
}

which is called after:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller       shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:@"All"];
    ...

with:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    [self.filteredListContent removeAllObjects];

    for (Store *store in [self.fetchedResultsController fetchedObjects])
    {
        if ([scope isEqualToString:@"All"] || [store.name isEqualToString:scope])
        {
            NSComparisonResult result = [store.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame)
            {
                [self.filteredListContent addObject:store];
            }
        }
    }
}

which is taken from the Apple TableSearch example.

The initial problem is twofold:

1) self.tableView is not like self.searchDisplayController.searchResultsTableView is ready to go

2) when searching indexPath (string) is always 0.

, didSelectRow... , , ... ?! , didSelectRow... , . , indexPath didSelectRow... , segue prepFor...:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    Store *storeToDetail = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        storeToDetail = [self.filteredListContent objectAtIndex:indexPath.row];
    }
    else
    {
        storeToDetail = [self.fetchedResultsController objectAtIndexPath:indexPath];
    }

    // How to get segue destination controller and set object?!
    // Before doing:
    [self performSegueWithIdentifier:@"StoreDetails" sender:self];
}

. , , , .

!

+2
3

sender prepareForSegue:sender: , .

, Store, , .

- ,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FooCell *cell = (FooCell *)[self.tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER];
    Foo *foo = [self fooForIndexPath:indexPath];

    cell.foo = foo;
    // additional initialization

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"view detail"]) {
        DetailViewController *destViewController = segue.destinationViewController;
        destViewController.foo = [(FooCell *)sender foo];
    }
}
+4

- (void) prepareForSegue: (UIStoryboardSegue *) segue : (id)

{

(self.filteredListContent)

{//NSLog (@ "post filter data";}

{//NSLog (@ "post all";}

}

+1

You are using the wrong test.

if (self.tableView == self.searchDisplayController.searchResultsTableView) {
  ...

This will always fail; you ask if different tables are different. The answer is always no.

However, you can check if the sender is from searchResultsTableView.

NSIndexPath *searchIndexPathOfSelectedCell = [self.searchDisplayController.searchResultsTableView indexPathForCell:sender];

if (searchIndexPathOfSelectedCell) {
  ...
0
source

All Articles