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

誰かが私がどこで間違っているのか教えてもらえますか?これは私をINSANEに駆り立てています!!!

4

1 に答える 1

0

これは、他のカスタム オブジェクトではなく、self.detailItemに応答しない定数文字列であるためです。objectForKey

本来あるべきクラス定義を投稿していないので、これ以上は言えません。

于 2012-12-10T16:42:48.023 に答える