0

我有一个完美运行的 TableView。现在我已经TableViewSearchBar 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]; This is my output Terminating app due to unaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0x243c8> valueForUndefinedKey:]: This class is not key value coding-compliant for key _rotulos.'

我遵循本教程,但在我的情况下,_rotulos 不是某些类的属性,是 NSArray。

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

4

1 回答 1

0

您不能使用predicateWithFormat:. predicateWithBlock:以其他方式使用或创建过滤后的数组(即 not filteredArrayUsingPredicate:)。

于 2014-02-21T18:03:20.433 回答