2010-10-21 76 views
0

我希望你能回答这个:iPhone SDK菜鸟问题与搜索栏

我曾经有过这样的数组:

NSArray *array = [NSArray arrayWithObjects:@"Object1", @"Object2", @"Object3", nil]; 
NSDictionary *arrayDictionaryDict = [NSDictionary dictionaryWithObject:array forKey:@"Key"]; 

[listOfItems addObject:arrayDictionaryDict]; 

现在我有这样的:

NSDictionary *dict1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Object1", @"Subtitle1", @"Object2", @"Subtitle2", @"Object3", @"Subtitle3", nil] forKeys:[NSArray arrayWithObjects:@"title", @"subtitle", @"title", @"subtitle", @"title", @"subtitle", nil]]; 

listOfItems = [[NSArray alloc] initWithObjects:dictA, nil]; 

我也有这种方法的搜索栏:

// in search the table view void 
[…] 
for (NSDictionary *dictionary in listOfItems) { 

    NSArray *array = [dictionary objectForKey:@"Key"]; 
[searchArray addObjectsFromArray:array]; 

} 

[B]我应该如何改变这种最后以适应新的数组和无论是在标题和副标题搜索方法?[/ B]

感谢名单

+0

只是一个简短的一面注意:NSDictionary不能容纳共享相同密钥的对象。你可以有一个对象用于键@“标题”和一个用于键@“副标题”,但不是三个。 – JustSid 2010-10-21 21:59:41

+0

我明白了。那么,我应该怎么做才能使表格视图分段使用可搜索的标题和每个单元格的字幕? – Gianluca 2010-10-22 08:48:42

+0

您可以创建包含NSDictionary的每一个与@“称号”,并@“副标题”一个NSArray键 – JustSid 2010-10-22 09:56:34

回答

0
NSArray *array = [dictionary allValues]; 

这将返回一个NSArray所有保存的值。如果我正确理解你的问题,这就是你正在寻找的。

0

是的,它是:)。不幸的是我仍然有一个问题:应用程序,一旦崩溃,因为我键入搜索栏的东西:\

这是调试错误:

2010-10-22 11:01:01.511 JGD Basic[543:207] -[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5836c 2010-10-22 11:01:01.517 JGD Basic[543:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5836c'

我试着使用,因为你告诉我,每个字典中只有一个带有“标题”键和一个用于“字幕”键的元素,但我仍然遇到此错误。这是我的“viewDidLoad”和“searchBar”代码。你能检查一下吗?

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSDictionary *dict1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Object1", @"Subtitle1", nil] 
                 forKeys:[NSArray arrayWithObjects:@"title", @"subtitle", nil]]; 

    listOfItems = [[NSArray alloc] initWithObjects:dict1, nil]; 

    //Initialize the copy array. 
    copyListOfItems = [[NSMutableArray alloc] init]; 

    //Set the title 
    self.navigationItem.title = @"Table View"; 

    //Add the search bar 
    self.tableView.tableHeaderView = searchBar; 
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 

    searching = NO; 
    letUserSelectRow = YES; 

    //Info Button 
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
    infoButton.frame = CGRectMake(0.0, 0.0, 50.0, 25.0); 
    infoButton.backgroundColor = [UIColor clearColor]; 
    [infoButton addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside]; 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton]; 

} 

// Info 
-(void)showInfo:(id)sender{ 

    InfoController *infoPage = [[InfoController alloc] init]; 
    UINavigationController *n = [[UINavigationController alloc] initWithRootViewController:infoPage] ; 
    [infoPage release]; 

    [infoPage setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal]; 
    [self presentModalViewController:n animated:YES]; 

} 

/**********************/ 
/***** Search Bar *****/ 
/**********************/ 

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar { 
    if(searching) 
     return; 

    searching = NO; 
    letUserSelectRow = YES; 
} 

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText { 

    //Remove all objects first. 
    [copyListOfItems removeAllObjects]; 

    if([searchText length] > 0) { 

     searching = YES; 
     letUserSelectRow = YES; 
     self.tableView.scrollEnabled = YES; 
     [self searchTableView]; 
    } 

    else { 

     searching = NO; 
     letUserSelectRow = NO; 
     self.tableView.scrollEnabled = NO; 
    } 

    [self.tableView reloadData]; 

} 

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar { 
    [self searchTableView]; 
} 

- (void) searchTableView { 

    NSString *searchText = searchBar.text; 
    NSMutableArray *searchArray = [[NSMutableArray alloc] init]; 

    for (NSDictionary *dictionary in listOfItems) 
    { 
     NSArray *array = [dictionary allValues]; 
     [searchArray addObjectsFromArray:array]; 
    } 
    for (NSString *sTemp in searchArray) 
    { 
     NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if (titleResultsRange.length > 0) 
      [copyListOfItems addObject:sTemp]; 
    } 

    [searchArray release]; 
    searchArray = nil; 
} 
+0

对不起,看不到错误。然而,问题在于你想调用一个不起作用的NSString上的objectForKey:。 – JustSid 2010-10-22 09:58:35