0

我目前正試圖讓一個搜索顯示控制器延續到一個細節視圖,但沒有太多的運氣!我有搜索工作和mainTableView(原始表格視圖)與標題和圖像相關,但是當我嘗試從searchdisplaytableview繼續時,它會崩潰。它在細節視圖崩潰在此階段:- [__ NSCFConstantString objectForKey:]:無法識別的選擇器發送到實例0x21eb4

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 

請能有人告訴我,我要去哪裏錯了嗎?這讓我瘋狂!

回答

0

這是因爲self.detailItem是一個常量字符串,它不響應objectForKey,而不是其他一些自定義對象。

你還沒有發佈它應該是什麼類的定義,所以我不能告訴你更多。

+0

你是怎麼定義它的定義?對不起,我是新手?你還需要在我的代碼中看到什麼,我將編輯我的原始文章 – David

+0

我不知道你認爲你在'self.detailItem'中存儲了什麼類型的對象。你可以使用'id'類型,它不能幫助你記住。 – trojanfoe

+0

我編輯了原始帖子以顯示我的所有代碼(減去大多數產品,因爲您不需要查看所有代碼就可以瞭解其中的內容)。 這只是我被證明從表格視圖中延伸出來的方式。它適用於第一個表格視圖,但不適用於結果 – David

相關問題