uisearchbar in grouped section uitable

2019-07-29 04:52发布

问题:

I've pieced together several tutorials to create a grouped table with sections and I'm now trying to get a uisearchbar to work. the problem I'm having is how to search within the grouped sections.

I've read the similar questions this post suggested but can't

This is the code to create the grouped sections

#import "Job.h"      // A model for the data
#import "Address.h"  // Another model for the data

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.theTable.delegate = self;
    self.theTable.dataSource =self;

    _searchBar.delegate = (id)self;

    FMDBDataAccess *db = [[FMDBDataAccess alloc] init];

    jobs = [[NSMutableArray alloc] init];
    jobs = [db getJobs:1];

    _sections = [[NSMutableDictionary alloc] init];

    NSMutableArray *jobsTempArray = [db getJobsAsDictionary:1];

    BOOL found;

    // Loop through the books and create our keys
    for (NSDictionary *book in jobsTempArray)
    {
        NSString *cLong = [book objectForKey:@"addrAddress"];

        NSString *c = [cLong substringToIndex:1];

        found = NO;

        for (NSString *str in [_sections allKeys])
        {
            if ([str isEqualToString:c])
            {
                found = YES;
            }
        }

        if (!found)
        {
            [_sections setValue:[[NSMutableArray alloc] init] forKey:c];
        }

    }

    // Loop again and sort the books into their respective keys
    for (NSDictionary *book in jobsTempArray)
    {
        [[_sections objectForKey:[[book objectForKey:@"addrAddress"] substringToIndex:1]] addObject:book];
    }

    // Sort each section array
    for (NSString *key in [_sections allKeys])
    {
        [[_sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"addrAddress" ascending:YES]]];
    }

}

And this is the code that searches

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
    if(text.length == 0)
    {
        _isFiltered = FALSE;
    }
    else
    {
        _isFiltered = true;
        _filteredjobs = [[NSMutableArray alloc] init];

        //for (Job* book in jobs)
        //for (Job* book in [_sections allKeys])
        //for (NSString *food in [_sections allKeys])
        for (NSDictionary* book in [_sections allKeys])
        {
            NSString *addrStr = [book objectForKey:@"addrAddress"];
            NSString *postStr = [book objectForKey:@"addrPostcode"];

            //NSRange nameRange = [book.jobAddress rangeOfString:text options:NSCaseInsensitiveSearch];
            NSRange nameRange = [addrStr rangeOfString:text options:NSCaseInsensitiveSearch];

            //NSRange descriptionRange = [book.jobPostcode rangeOfString:text options:NSCaseInsensitiveSearch];
            NSRange descriptionRange = [postStr rangeOfString:text options:NSCaseInsensitiveSearch];

            if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
            {
                [_filteredjobs addObject:book];
            }
        }
    }

    [self.theTable reloadData];
}

I've got as far as realising I need to change for (Job* food in jobs) to for (NSDictionary* book in [_sections allKeys]) but I'm stuck how to search within [_sections allKeys]

Specifically this line

NSRange nameRange = [addrStr rangeOfString:text options:NSCaseInsensitiveSearch];

which crashes with

-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x692e200

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x692e200':

Any ideas? PS Treat me as a noob, I'll need some code as well as explanation - I'm still learning obj-c

回答1:

Check the Link.It shows the UISearchBar With Grouped Section Tableview.Its a simple Tutorial..Hope its useful for you



回答2:

I found the answer in UISearchBar - search a NSDictionary of Arrays of Objects and reading up on allkeys.

Basically loop through the grouped NSDictionary and extract the NSArrays, then loop through again searching...

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
    if(text.length == 0)
    {
        _isFiltered = FALSE;
    }
    else
    {
        _isFiltered = true;
        _filteredjobs = [[NSMutableArray alloc] init];

        NSString *currentLetter = [[NSString alloc] init];

        for (int i=0; i<[_sections count]; i++)
        {
            currentLetter = [[_sections allKeys] objectAtIndex:i];

            NSArray *jobsForKey = [ [NSArray alloc] initWithArray:[_sections objectForKey:[[_sections allKeys] objectAtIndex:i]] ];

            for (int j=0; j<[jobsForKey count]; j++)
            {
                NSDictionary *book = [jobsForKey objectAtIndex:j];
                NSRange titleResultsRange = [[book objectForKey:@"addrAddress"] rangeOfString:text options:NSCaseInsensitiveSearch];

                if(titleResultsRange.location != NSNotFound)
                {
                    [_filteredjobs addObject:book];
                }
            }
        }

    }

    [self.theTable reloadData];
}