繁体   English   中英

使用来自plist的数据的Objective-C过滤器表格视图

[英]objective-c filter table view with data from plist

我基于示例编写了代码。 初始数据显示在表格视图中,但是我不确定如何实现其余数据以显示过滤后的数据。 我没有错误,搜索栏有效,但没有过滤数据。 这是我的plist XML:

<dict>
    <key>Things to do</key>
    <array>
        <dict>
            <key>activity</key>
            <string>Watch movies or TV shows</string>
            <key>keywords</key>
            <array>
                <string>tv shows</string>
                <string>movies</string>
            </array>
        </dict>
        <dict>
            <key>activity</key>
            <string>Go Hiking</string>
            <key>keywords</key>
            <array>
                <string>hiking</string>
                <string>mountains</string>
            </array>
        </dict>
        <dict>
            <key>activity</key>
            <string>Video Games</string>
            <key>keywords</key>
            <array>
                <string>video games</string>
                <string>playstation</string>
                <string>xbox</string>
                <string>nintendo</string>
            </array>
        </dict>
    </array>
</dict>

这是我的CategoriesViewController.m:

@interface CategoriesViewController () <UISearchDisplayDelegate>

@property (nonatomic, strong) NSMutableArray *tableData;
@property (nonatomic, strong) NSMutableArray *searchResults;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@end

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"categories" ofType:@"plist"];
NSDictionary *categoriesDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

self.tableData = categoriesDictionary[@"Things to do"];
//I think this next line is supposed to make it appear on top of the original table
self.searchResults = [NSMutableArray arrayWithCapacity:[self.tableData count]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = self.tableData[indexPath.row][@"activity"];
    }

    return cell;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{   
    [self.searchResults removeAllObjects];

    NSPredicate *resultPredicate = [NSPredicate
    predicateWithFormat:@"SELF contains[cd] %@",
    searchText];

    self.searchResults = [NSMutableArray arrayWithArray:[self.tableData filteredArrayUsingPredicate:resultPredicate]];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
    scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
    objectAtIndex:[self.searchDisplayController.searchBar
    selectedScopeButtonIndex]]];
    return YES;
}

就像示例一样,除了我更改了数据的来源。 如何根据关键字搜索? 在此示例中,我是否仅搜索初始表视图中包含的内容? 谢谢你的时间...

没有更多代码很难说,但是我认为您需要基于搜索字符串显示/隐藏两个表视图。

如果字符串为空,这将隐藏您的searchResultsTableView并显示其他表视图,我正在whatEverYouCalledYourOtherTableView调用whatEverYouCalledYourOtherTableView

如果字符串不为空,则进行搜索,显示searchResultsTableView ,隐藏whatEverYouCalledYourOtherTableView并重新加载searchResultsTableView的数据。

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    if ([searchString isEqualToString:@""])
    {
        self.searchDisplayController.searchResultsTableView.hidden = YES;
        self.searchDisplayController.whatEverYouCalledYourOtherTableView.hidden = NO;
    }
    else
    {
        [self filterContentForSearchText:searchString
                                   scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                           objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];


        self.searchDisplayController.searchResultsTableView.hidden = NO;
        self.searchDisplayController.whatEverYouCalledYourOtherTableView.hidden = YES;
        [self.searchDisplayController.searchResultsTableView reloadData];
    }
    return YES;
}

另外,您的filterContentForSearchText是错误的。 您有一个字典数组而不是字符串。 首先只抓住字符串,然后运行搜索。 您最好立即创建一个字符串字典,使搜索生效的最简单方法是这样。

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{   
    [self.searchResults removeAllObjects];

    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];

    NSMutableArray *activityArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < [self.tableData count]; i++)
    {
        [activityArray addObject:self.tableData[i][@"activity"]];
    }

    self.searchResults = [NSMutableArray arrayWithArray:[activityArray filteredArrayUsingPredicate:resultPredicate]];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM