0

私はこのplistを持っています:

<?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 に答える