views:

47

answers:

2

Hello. I hope you will reply to this:

I once had this kind of array:

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

[listOfItems addObject:arrayDictionaryDict];

Now I have this:

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];

I also had a search bar with this method:

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

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

}

[B]How should I change this last method to fit the new Array and search both in the title and subtitle?[/B]

Thanx

A: 
NSArray *array = [dictionary allValues];

This returns an NSArray with all saved values. If I understand your question correctly, this is what you are looking for.

JustSid
A: 

Yes, it is :). Unfortunately I still have a problem: the app crashes as soon as I type something in the searchbar :\

This is the Debugger Error:

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'

I've tried to use, as you told me, just one element with key "title" and one for "subtitle" key in each dictionary, but I still get this error. Here's my "viewDidLoad" and "searchBar" code. Can you check it out, please?

- (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;
}
Gianluca
Sorry, can't see the error. However, the problem is that you want to invoke objectForKey: on an NSString which doesn't work.
JustSid