【问题标题】:UISearchBar and TableViewControllerUISearchBar 和 TableViewController
【发布时间】:2014-02-21 17:52:48
【问题描述】:

我有一个完美运行的 TableView。 现在我在TableView 中包含了一个搜索SearchBar component and Display ViewController。 我希望根据 NSArray _rotulos 填充的标签进行搜索。

@property (nonatomic, strong) NSArray *rotulos;

我在viewDidLoad方法中填充内容(对象比较多,这里只是一个例子)

_rotulos = @[@"Item 1", @"Item 2", @"Item 3", @"Block 1", @"Block 2",@"Block 3"];

我在 cellForRowAtIndexPath 方法中填充

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TableCell";
    TableCell *cell = (TableCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //Configure the cell...
       if (cell == nil) {
            cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

    long row = [indexPath row];

    cell.rotuloLabel.text    = _rotulos[row];
    cell.descricaoLabel.text = _vinicola[row];
    cell.uvaLabel.text       = _uva[row];

    return cell;
}

这很好用。 单击 SearchBar 并开始输入时出现问题。

这是进行此搜索的代码。

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"_rotulos contains[c] %@", searchText];
    searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}

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

    return YES;
}

我在NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"_rotulos contains[c] %@", searchText]; 行有问题 这是我的输出 Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__nscfconstantstring> valueForUndefinedKey:]: 这个类不是键值编码兼容键 _rotulos。'

我关注这个Tutorial,但在我的情况下,_rotulos 不是某个类的属性,是一个 NSArray。

我该如何解决? 抱歉,如果忘记发布内容。

【问题讨论】:

    标签: ios7 uitableview uisearchbar uisearchbardisplaycontrol


    【解决方案1】:

    您无法使用predicateWithFormat: 形成此过滤器。使用predicateWithBlock: 或以其他方式创建过滤后的数组(即不是filteredArrayUsingPredicate:)。

    【讨论】:

    • 我该怎么做?我在找一些例子,但我没有找到。
    • 我不知道,因为我不明白您要做什么。但是例如看一下这段代码:github.com/mattneub/Programming-iOS-Book-Examples/blob/master/… 查看filterData: 实现,看看我是如何根据用户在搜索字段中输入的内容为我的表形成过滤数组的。
    • 我知道。但我不知道您要过滤该 NSArray 的基础。但是,我不需要,因为我不会为您编写代码。 看看我指向你的代码。我正在向你展示这是如何完成的。您应该能够轻松地适应该示例。
    猜你喜欢
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    相关资源
    最近更新 更多