0

I am currently trying to get a search display controller to segue to a detail view but not having much luck! I have the search working and the mainTableView(original table view) seguing with the title and image but it crashes when I try to segue from the searchdisplaytableview. It is crashing at the detail view at this stage:

self.navigationItem.title = [self.detailItem objectForKey:@"name"];

The full code for the detail view is:

.h

#import <UIKit/UIKit.h>

@interface SearchDetailViewController : UIViewController<UISplitViewControllerDelegate>


@property (strong, nonatomic) id detailItem;

@property (strong, nonatomic) IBOutlet UIImageView *detailImage;

@end

.m

#import "SearchDetailViewController.h"

@interface SearchDetailViewController ()

@end

@implementation SearchDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }
}

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        self.navigationItem.title = [self.detailItem objectForKey:@"name"];
        [self.detailImage setImage:[UIImage imageNamed:self.detailItem[@"image"]]];
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

the code for the search is: .h

#import <UIKit/UIKit.h>

@class SearchDetailViewController;

@interface SearchWillWorkViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>
{
    NSMutableArray *graniteArray;
    NSMutableArray *marbleArray;
    NSMutableArray *quartzArray;
    NSMutableArray *silestoneArray;
    NSArray *searchRowSelected;

    NSMutableArray *sectionArray;

    UITableView *mainTableView;

    NSMutableArray *contentsList;
    NSMutableArray *searchResults;
    NSString *savedSearchTerm;

}

@property (strong, nonatomic) SearchDetailViewController *detailViewController;

@property (nonatomic, strong) IBOutlet UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *contentsList;
@property (nonatomic, strong) NSMutableArray *searchResults;
@property (nonatomic, copy) NSString *savedSearchTerm;
@property (strong, nonatomic) NSArray *stoneSections;


- (void)handleSearchForTerm:(NSString *)searchTerm;
- (void)createStoneData;


@end

.m

#import "SearchWillWorkViewController.h"
#import "SearchDetailViewController.h"

@interface SearchWillWorkViewController ()

@end

@implementation SearchWillWorkViewController

@synthesize mainTableView;
@synthesize contentsList;
@synthesize searchResults;
@synthesize savedSearchTerm;
@synthesize stoneSections;
@synthesize detailViewController = _detailViewController;

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Save the state of the search UI so that it can be restored if the view is re-created.
    [self setSavedSearchTerm:[[[self searchDisplayController] searchBar] text]];

    [self setSearchResults:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createStoneData];

    // Restore search term
    if ([self savedSearchTerm])
    {
        [[[self searchDisplayController] searchBar] setText:[self savedSearchTerm]];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[self mainTableView] reloadData];
}

- (void)createStoneData {

    self.stoneSections=[[NSArray alloc] initWithObjects:
                        @"Granite",@"Marble",@"Quartz",@"Silestone",nil];

    graniteArray = [[NSMutableArray alloc] init];
    marbleArray = [[NSMutableArray alloc] init];
    quartzArray = [[NSMutableArray alloc] init];
    silestoneArray = [[NSMutableArray alloc] init];

    [graniteArray addObject:[[NSMutableDictionary alloc]
                             initWithObjectsAndKeys:@"Angel Cream", @"name",
                             @"angel-cream.jpg", @"image", nil]];

    [marbleArray addObject:[[NSMutableDictionary alloc]
                            initWithObjectsAndKeys:@"Arabescato", @"name",
                            @"arabescato.jpg", @"image", nil]];

    [quartzArray addObject:[[NSMutableDictionary alloc]
                            initWithObjectsAndKeys:@"Caesarstone: Black Knight", @"name",
                            @"Black Knight.jpg", @"image", nil]];

    [silestoneArray addObject:[[NSMutableDictionary alloc]
                               initWithObjectsAndKeys:@"Cielo: Aluminio Nube", @"name",
                               @"silestone-aluminio-nube.jpg", @"image", nil]];

    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:graniteArray,marbleArray,quartzArray,silestoneArray, nil];
    [self setContentsList:array];

}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)handleSearchForTerm:(NSString *)searchTerm
{
    [self setSavedSearchTerm:searchTerm];

    if ([self searchResults] == nil)
    {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [self setSearchResults:array];
    }

    [[self searchResults] removeAllObjects];

    if ([[self savedSearchTerm] length] != 0)
    {
        for (NSMutableArray *array in contentsList)
        {
            for (NSDictionary* dictionary in array)
            {
                NSString *currentstring = [dictionary objectForKey:@"name"];
                NSRange r = [currentstring rangeOfString:searchTerm options:NSCaseInsensitiveSearch];
                if (r.location != NSNotFound) {
                    [[self searchResults] addObject:currentstring];
                }
            }
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    if (tableView == [[self searchDisplayController] searchResultsTableView])
        return 1;
    else
        return [self.stoneSections count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (tableView == [[self searchDisplayController] searchResultsTableView])
        return nil;
    else
        return [self.stoneSections objectAtIndex:section];
}

#pragma mark -
#pragma mark UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rows;

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        rows = [[self searchResults] count];
    else
        rows = [[self.contentsList objectAtIndex:section] count];


    return rows;
}

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

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

    NSString *contentForThisRow = nil;

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        contentForThisRow = [self.searchResults objectAtIndex:indexPath.row];
    else
        contentForThisRow = [[[[self contentsList] objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];

    cell.textLabel.text = contentForThisRow;

    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate Methods

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
            if (self.searchDisplayController.active) {
        [self.detailViewController setDetailItem:[self.searchResults objectAtIndex:indexPath.row]];
}
    else
        [self.detailViewController setDetailItem:[[contentsList objectAtIndex:indexPath.section]
                                                  objectAtIndex: indexPath.row]];

}

#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self handleSearchForTerm:searchString];

    // Return YES to cause the search result table view to be reloaded.

    return YES;
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    [self setSavedSearchTerm:nil];

    [[self mainTableView] reloadData];      
}

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

    if ([segue.identifier isEqualToString:@"detailview"]) {
        if (self.searchDisplayController.active) {
            self.detailViewController=segue.destinationViewController;
        }
        else
        self.detailViewController=segue.destinationViewController;
    }
}

@end

Please can someone tell me where I'm going wrong? This is driving me INSANE!!!

David
  • 319
  • 2
  • 5
  • 18

1 Answers1

0

It's because self.detailItem is a constant string, which doesn't respond to objectForKey, not some other custom object.

You haven't posted the class definition of what it's supposed to be, so I cannot tell you more.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • how do you mean it's definition? sorry I'm new to this? what else would you need to see in my code and I'll edit my original post – David Dec 10 '12 at 16:46
  • I don't know what type of object you think you are storing in `self.detailItem`. You use the type `id` which cannot help you remember. – trojanfoe Dec 10 '12 at 16:49
  • I have edited the original post to show all my code (minus most of the products as you don't need to see all of them to get the idea of whats going on in there). This is just the way I was shown to segue from a table view. It works fine for the first table view but not for the results – David Dec 10 '12 at 16:55