uisearchbar 在分组部分 uitable

Posted

技术标签:

【中文标题】uisearchbar 在分组部分 uitable【英文标题】:uisearchbar in grouped section uitable 【发布时间】:2013-03-12 23:37:43 【问题描述】:

我拼凑了几个教程来创建一个包含部分的分组表,我现在正试图让一个 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把我当作菜鸟,我需要一些代码和解释——我还在学习obj-c

【问题讨论】:

【参考方案1】:

检查Link.它显示了带有分组部分表格视图的 UISearchBar。它是一个简单的教程..希望它对你有用

【讨论】:

感谢链接,但它在搜索后构造了分组部分,但搜索了一个已经分组的 NSDictionary(实际上是基于我已经使用的教程)【参考方案2】:

我找到了答案 UISearchBar - search a NSDictionary of Arrays of Objects 并阅读 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 在分组部分 uitable的主要内容,如果未能解决你的问题,请参考以下文章

UITables 部分标题中的多行文本

更改 UITable 部分背景颜色而不丢失部分标题

使用 IOS9 时,无法在 UITable 的部分标题上的 UIButtons 上触发触摸事件(IOS10 很好)

IOS:UISearchBar 部分覆盖 UINavigationBar 提示

UISearchBar 表部分索引标题重叠

iPhone UITableView 与 UISearchBar 和刷新部分索引