0

我目前正在尝试让搜索显示控制器转到详细视图,但运气不佳!我的搜索工作正常,mainTableView(原始表格视图)与标题和图像进行了分割,但是当我尝试从 searchdisplaytableview 分割时它崩溃了。它在此阶段的详细视图中崩溃:

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

详细视图的完整代码是:

。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

搜索代码是:.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

请问有人能告诉我哪里出错了吗?这让我发疯了!!!

4

1 回答 1

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.

于 2012-12-10T16:42:48.023 回答