在UITableViewController中遇到UISearchBar问题 [英] Having Problems With UISearchBar in UITableViewController
问题描述
我正在尝试将UISearchBar添加到我的UITableView。
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;
}
但我没有字典,我只想搜索我的字典数组'exerciseArray'是这样的初始化:
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];
当我NSLog它时,我得到:
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.
我试过这个但它没有工作并且崩溃了 NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
I tried this but it did not work and crashed at line 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];
}
}
}
推荐答案
我相信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
这篇关于在UITableViewController中遇到UISearchBar问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!