2012-08-06 66 views
0

我正在一個應用程序,我有兩個模型,在視圖控制器我想進行搜索,並從兩個模型中找到匹配搜索關鍵字的實例,並在tableview中列出它們。我還使用RestKit將對象存儲在覈心數據中。我以後也想在我第一次進行本地搜索之後用服務器結果更新搜索結果。CoreData搜索RestKit

我現在的 - 也許天真 - 嘗試看起來是這樣的:

- (void)search 
{ 

    if (self.searchField.text !=nil) { 
     NSPredicate *predicate =[NSPredicate predicateWithFormat:@"post contains[cd] %@", self.searchField.text]; 
     self.posts = [[Post findAllWithPredicate:predicate] mutableCopy]; 

     // reload the table view 
     [self.tableView reloadData]; 

    }  

    [self hideKeyboard]; 

} 

這將導致錯誤:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (post CONTAINS[cd] "t")' 

的的tableView的cellForRowAtIndexPath委託方法是這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) { 
     cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    Post *post = [self.posts objectAtIndex:indexPath.row]; 
    cell.titleLabel.text = post.title; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 
} 

關於如何搜索這樣的應該看起來的任何想法將是偉大的!

回答

2

您可能想看看RKGitHub示例項目,它展示了RKTableController類的功能。

至於錯誤,我不認爲它與你的問題有關。

+0

問題主要在於我的謂詞,使用NSCompoundPredicate解決了大部分問題。我也誤解了謂詞的運作方式。無論如何,感謝您的回答,鏈接看起來很酷。 – Anders 2012-08-08 08:26:47