UITableViewController 中的 UISearchBar 出现问题

发布于 2024-11-17 08:25:09 字数 2026 浏览 3 评论 0原文

我正在尝试将 UISearchBar 添加到我的 UITableView 中。

这是他们用来搜索的方法:

- (void) searchTableView {

NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

for (NSDictionary *dictionary in listOfItems)
{
NSArray *array = [dictionary objectForKey:@"Countries"];
[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;
}

但是我没有字典,我只想在我的数组“exerciseArray”中搜索,它的初始化如下:

NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.exerciseArray = rootLevel;
[rootLevel release];

当我对其进行 NSLog 时,我得到:

exerciseArray: (
        {
        exerciseName = "Balance Board";
    },
        {
        exerciseName = "Barbell Seated Calf Raise";
    },
        {
        exerciseName = "Calf Press On The Leg Press Machine";
    },
        {
        exerciseName = "Calf Raises- With Bands";
    },

那么任何人都可以帮我修改适合我的数组的搜索方法?里面好像有一把钥匙。

我尝试了这个,但它不起作用,并在 NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch]; 行崩溃

,并出现错误 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSCFDictionary rangeOfString:options:]:无法识别的选择器发送到实例0x2d6450

- (void) searchTableView {

    NSString *searchText = searchBar.text;
    NSLog(@"exerciseArray: %@", exerciseArray);
    for (NSString *sTemp in listOfItems)
    {
        if (sTemp) {
            NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (titleResultsRange.length > 0)
                [copyListOfItems addObject:sTemp];
        }
    }
}

I am trying to add a UISearchBar to my UITableView.

This is the method they use to search:

- (void) searchTableView {

NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

for (NSDictionary *dictionary in listOfItems)
{
NSArray *array = [dictionary objectForKey:@"Countries"];
[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;
}

But I don't have a dictionary, I just want to search in my array 'exerciseArray' which is init like this:

NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.exerciseArray = rootLevel;
[rootLevel release];

When I NSLog it, I get:

exerciseArray: (
        {
        exerciseName = "Balance Board";
    },
        {
        exerciseName = "Barbell Seated Calf Raise";
    },
        {
        exerciseName = "Calf Press On The Leg Press Machine";
    },
        {
        exerciseName = "Calf Raises- With Bands";
    },

So can anyone help me modify the search method to fit the array I have? It seems to have a key in it.

I tried this but it did not work and crashed at line NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

with error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x2d6450

- (void) searchTableView {

    NSString *searchText = searchBar.text;
    NSLog(@"exerciseArray: %@", exerciseArray);
    for (NSString *sTemp in listOfItems)
    {
        if (sTemp) {
            NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (titleResultsRange.length > 0)
                [copyListOfItems addObject:sTemp];
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋千易 2024-11-24 08:25:09

看起来您有一组字典,每个字典中都有一个键/值对 (exerciseName)。所以对于你的数据集,它应该是这样的:

for (NSDictionary *dictionary in exerciseArray)
{
    NSArray *array = [dictionary objectForKey:@"exerciseName"];
    [searchArray addObjectsFromArray:array];
}
...

如果你发布与数组相关的 VC 代码以及你的 tableview 数据源方法,我可以修改它并向你展示你需要做什么。

Looks like you have an array of dictionaries with one key/value pair in each dictionary (exerciseName). so for your dataset, it should be this:

for (NSDictionary *dictionary in exerciseArray)
{
    NSArray *array = [dictionary objectForKey:@"exerciseName"];
    [searchArray addObjectsFromArray:array];
}
...

If you post your VC code related to the array and also your tableview datasource methods, I have can modify it and show you what you need to do.

毁梦 2024-11-24 08:25:09

为了从表视图中搜索,我尝试过这个,并且取得了成功,希望它能对您有所帮助......

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.searching=NO;
    NSString *originalString;
    [self.filteredProductArray removeAllObjects];
    for (int i=0; i<self.exerciseArray.count; i++)
    {
        originalString=[[[self.exerciseArray objectAtIndex:i] objectForKey:@"exerciseName"] lowercaseString];
        if (!([originalString rangeOfString:[searchText lowercaseString]].location==NSNotFound))
        {
            [self.filteredProductArray addObject:[self.exerciseArray objectAtIndex:i]];
        }
    }
    if ([searchText isEqual:@""])
    {
        self.searching=NO;
    }
    [self.tableView reloadData];
}

For searching from table view i have tried this and i got success hope it will help you......

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.searching=NO;
    NSString *originalString;
    [self.filteredProductArray removeAllObjects];
    for (int i=0; i<self.exerciseArray.count; i++)
    {
        originalString=[[[self.exerciseArray objectAtIndex:i] objectForKey:@"exerciseName"] lowercaseString];
        if (!([originalString rangeOfString:[searchText lowercaseString]].location==NSNotFound))
        {
            [self.filteredProductArray addObject:[self.exerciseArray objectAtIndex:i]];
        }
    }
    if ([searchText isEqual:@""])
    {
        self.searching=NO;
    }
    [self.tableView reloadData];
}
抱着落日 2024-11-24 08:25:09

我相信 NSPredicates 是在数组中搜索的好方法..您可以参考此链接.. 使用 NSPredicate 根据 NSDictionary 键过滤 NSArray

I believe NSPredicates are good way to search in arrays.. You can refer to this link.. Using NSPredicate to filter an NSArray based on NSDictionary keys

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文