在的UISearchBar分组节uitable(uisearchbar in grouped sec

2019-10-17 19:18发布

我拼凑几个教程创建具有段分组的表,现在我试图让一个UISearchBar工作。 我遇到的问题是如何将分组段中进行搜索。

我读过这篇文章提出的类似的问题,但不能

这是创建分组的部分代码

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

}

这是搜索代码

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

据我得为实现我需要改变for (Job* food in jobs) ,以for (NSDictionary* book in [_sections allKeys])但我坚持了如何在搜索[_sections allKeys]

具体地,本线

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

这与崩溃

- [__ NSCFString objectForKey:]:无法识别的选择发送到实例0x692e200

***终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因: ' - [__ NSCFString objectForKey:]:无法识别的选择发送到实例0x692e200':

有任何想法吗? PS把我当作一个小白,我需要一些代码,以及解释 - 我仍然在学习对象 -

Answer 1:

检查链接。 它显示的UISearchBar使用分组第Tableview.Its简单Tutorial..Hope其对您有用



Answer 2:

我找到了答案的UISearchBar -搜索对象数组的NSDictionary中和allkeys读了。

基本上遍历NSDictionary的分组,并通过再次搜索提取NSArrays,然后循环...

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


文章来源: uisearchbar in grouped section uitable