如何通过 Interface Builder 在 Objective-C 中实现搜索栏

Posted

技术标签:

【中文标题】如何通过 Interface Builder 在 Objective-C 中实现搜索栏【英文标题】:How to Implement the Search Bar in Objective-C via Interface Builder 【发布时间】:2012-10-29 20:32:27 【问题描述】:

我有这个列表:

<?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>

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

#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。

提前致谢!

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

以上是关于如何通过 Interface Builder 在 Objective-C 中实现搜索栏的主要内容,如果未能解决你的问题,请参考以下文章

UIView 的子类。如何通过 Interface Builder 添加?

如何在不通过 Interface Builder 获取 UIButton 类的背景的情况下添加图像?

如何在 Xcode Interface Builder 中显示占位符背景?

如何通过编写 Objective-C 代码来动态更改 Interface Builder xib 文件?

如何像 Interface Builder 一样调整 iOS UI 组件的大小

如何指定我希望 UI 元素在 Interface Builder 中进行方向更改的位置?