0

我有这个列表:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>City 1</key>
    <array>
        <dict>
            <key>area</key>
            <string>City 1</string>
            <key>shop</key>
            <string>Shop 1</string>
            <key>location</key>
            <string>Location 1</string>
            <key>coordinates</key>
            <string>14.66592, 121.03139</string>
        </dict>
        <dict>
            <key>area</key>
            <string>City 1</string>
            <key>shop</key>
            <string>Shop 2</string>
            <key>location</key>
            <string>Location 2</string>
            <key>coordinates</key>
            <string>14.67097, 121.03766</string>
        </dict>
    </array>
    <key>City 2</key>
    <array>
        <dict>
            <key>area</key>
            <string>City 2</string>
            <key>shop</key>
            <string>Shop 1</string>
            <key>location</key>
            <string>Location 1</string>
            <key>coordinates</key>
            <string>14.65549, 120.98280</string>
        </dict>
        <dict>
            <key>area</key>
            <string>City 2</string>
            <key>shop</key>
            <string>Shop 2</string>
            <key>location</key>
            <string>Location 2</string>
            <key>coordinates</key>
            <string>14.65549, 120.98280</string>
        </dict><dict>
            <key>area</key>
            <string>City 2</string>
            <key>shop</key>
            <string>Shop 3</string>
            <key>location</key>
            <string>Location 3</string>
            <key>coordinates</key>
            <string>14.65549, 120.98280</string>
        </dict>
    </array>
</dict>
</plist>

如何在我的表格视图中实现一个搜索栏,其中填充了标题为键“商店”的行?这是我的搜索栏上的一些代码片段。

#pragma mark - Custom methods

- (void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];
    self.searchResultsArray = [self.theArray filteredArrayUsingPredicate:resultPredicate];
}

#pragma mark - SearchDisplayController methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    return YES;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text]
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:searchOption]];

    return YES;
}

我对如何定义搜索栏用于查找商店名称的“theArray”数组感到困惑。我什至尝试创建另一个包含所有商店名称的 plist 文件,但它给了我一个错误:

NSRangeException',原因:'-[__NSCFArray objectAtIndex:]:索引(1)超出范围(1)

所以我选择不使用这种方法并坚持我现有的 plist。

提前致谢!

4

1 回答 1

0

SELF搜索所有数组,将其更改为shop。

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

于 2013-12-01T18:18:09.087 回答