选项卡栏控制器上的TableView UISearchBar在搜索时崩溃 [英] TableView UISearchBar on Tab Bar Controller Crashes while Searching

查看:77
本文介绍了选项卡栏控制器上的TableView UISearchBar在搜索时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩我的应用程序表视图的搜索工具一段时间,现在试图让它工作但我在控制台中一直收到同样的错误。

I've been playing around with a search facility for my application table view for a while now trying to get it working but i keep getting the same error in my console.

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'[NSCFDictionary rangeOfString:options:]:无法识别的选择器发送到实例

我相信以下部分可能是我尝试在if语句中传递一些NSLog条目的问题,它似乎通过它但问题是当我点击搜索栏并开始输入时,我输入的第一个字母调用错误并取消我的应用。

I believe that this following section may be the problem I have tried passing some NSLog entries inside the if statement and it seems to get through it but the problem is when I click on the search bar and starting typing, the first letter I type calls the error and cancels my app.

问题出在哪里

在视图中将显示食物数组初始化如下:

In View Will Appear "Food" Array is initialized as below:

 NSString *myDBnew =@"/Users/taxsmart/Documents/rw3app.sql";

database = [[Sqlite alloc] init];

[database open:myDBnew];

NSString *quer = [NSString stringWithFormat:@"Select category from foodcat"];

Food = [database executeQuery:quer];

//[database executeNonQuery:quer];

[database close];

搜索栏委托方法遇到错误:

(void) searchTableView 

{

   NSString *searchText = searchBar.text;

   NSMutableArray *searchArray = [[NSMutableArray alloc] init];

//   [searchArray addObjectsFromArray:Food];

    for(NSDictionary *dictionary in Food)
    {
         NSString temp1 = [dictionary objectForKey:@"category"];
         [searchArray addObject:temp1];
    }

     for (NSString *sTemp in searchArray)

     {

              NSLog(@"Value: %@",NSStringFromClass([sTemp class]));

         NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

           if (titleResultsRange.length > 0)

               [copyListOfItems addObject:sTemp];
     }  

      [searchArray release];

       searchArray = nil;
}

我该怎么办?

请帮助。

请建议

谢谢

推荐答案

看起来数据库查询(Food)的结果是包含字典的字典。此代码:

It looks that result of database query (Food) is dictionary that contains dictionary. This code:

for(NSDictionary *dictionary in Food)
{
     NSString temp1 = [dictionary objectForKey:@"category"];
     [searchArray addObject:temp1];
}

可替换为:

for(NSDictionary *dictionary in Food)
{
     NSObject *ob = [dictionary objectForKey:@"category"];
     if([ob isKindOfClass: [NSString class]]) 
     {
        [searchArray addObject:ob];
     } 
     else if([ob isKindOfClass: [NSDictionary class]])
     {
        NSDictonary *dic1 = (NSDictionary*)ob;
        // ... at this point you can get the string for desired dictionary key 
        // or you can ignore it
     }
}

使用此代码,我们可以确保只将字符串放入searchArray。

With this code we can be sure that only strings are put into searchArray.

如果要对所需的键类别进行完整的树解析,那么您应该使用一些递归函数来搜索字典。

If you want to make full tree parsing for desired key 'category' then you should make some recursive function to search the dictionary.

您可以将Food变量转储到控制台,以查看哪个叶子实际上是您要查找的结果。把断点放到控制台类型'po Food'。

You can dump Food variable to console to see at which leaf is actually the result you are looking for. Put the break-point and into console type 'po Food'.

这篇关于选项卡栏控制器上的TableView UISearchBar在搜索时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆