Objective c 是否可以从UIButton更改谓词?

Objective c 是否可以从UIButton更改谓词?,objective-c,core-data,ios7,nspredicate,Objective C,Core Data,Ios7,Nspredicate,我在核心数据模型中有一组单词。我想有一些按钮,过滤器的基础上类别,如形容词,动词等 我有如下代码,但表不会重新加载 > - (IBAction)selectAdjectives:(id)sender { > NSLog(@"%s", __FUNCTION__); > predicateSwitch = 3; // an int > [NSFetchedResultsController deleteCacheWithName:nil]; >

我在核心数据模型中有一组单词。我想有一些按钮,过滤器的基础上类别,如形容词,动词等

我有如下代码,但表不会重新加载

> - (IBAction)selectAdjectives:(id)sender {
>     NSLog(@"%s", __FUNCTION__);
>     predicateSwitch = 3; // an int
>     [NSFetchedResultsController deleteCacheWithName:nil];
>     [self.wordTable reloadData]; }
> 


> - (NSPredicate*)predicate {
>     NSLog(@"%s", __FUNCTION__);
>     NSPredicate *predicate = [[NSPredicate alloc] init];
>     switch (predicateSwitch) {
>         case 0:
>             predicate = nil;
>             break;
>         
>         case 1:
>             ...
>             break;
>             
>         case2:
>             ...
>             
>         case3:
>             predicate = [NSPredicate predicateWithFormat:@"SELF.category contains[c] %@",@"Adjective"];
>             break;
>             
> 
>         default:
>             predicate = [NSPredicate predicateWithFormat:@"SELF.category contains[c] %@",@"Noun"];
>             break;
>     }
>     NSLog(@"Predicate: %@", predicate);
>     
>     return predicate; }
> 


> - (NSFetchedResultsController *)fetchedResultsController {
>     //NSLog(@"%s", __FUNCTION__);
>     
>     if (_fetchedResultsController != nil) {
>         return _fetchedResultsController;
>     }
>     
>     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
>     
>     NSEntityDescription *entity = [NSEntityDescription entityForName:@"WordEntity"
> inManagedObjectContext:self.managedObjectContext];
>     [fetchRequest setEntity:entity];
>     [fetchRequest setPredicate:[self predicate]];
>           // Create the sort descriptors array.    ...
>      
>     NSArray *sortDescriptors = @[sortDescriptor];
>     [fetchRequest setSortDescriptors:sortDescriptors];
>     
>     // Create and initialize the fetch results controller.
>     _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
> managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
> cacheName:nil];
>     
>     _fetchedResultsController.delegate = self;
>     
>     return _fetchedResultsController; }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //NSLog(@"%s", __FUNCTION__);

    static NSString *CellIdentifier = @"WordCell";

    UITableViewCell *cell = (UITableViewCell *)[self.wordTable dequeueReusableCellWithIdentifier:CellIdentifier];

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

    WordEntity *word = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        word = [self.searchResults objectAtIndex:indexPath.row];
        count = self.searchResults.count;
        self.numberWordsLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)count];

    } else {
        word = [self.fetchedResultsController objectAtIndexPath:indexPath];
        self.numberWordsLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)fullCount];
    }


        cell.textLabel.text =  .....;
        cell.detailTextLabel.text = .....;
我得到了整个表,但是谓词没有触发,表视图也没有重新加载。如果您能给我提些建议,我将不胜感激。

有几件事不对

请参阅switch语句。案例2和案例3中缺少空格

你不能只是重新加载表格。您必须重新加载视图控制器。。我不喜欢这样,所以我愿意接受其他选择

你不能只是用

[NSFetchedResultsController deleteCacheWithName:nil]

你必须敲打它:

self.fetchedResultsController = nil;
因此:


在你发布的代码中没有任何东西可以使用切换谓词。我认为这行。。。获取请求集谓词:[自谓词]。
- (IBAction)selectNouns:(id)sender {
    //NSLog(@"%s", __FUNCTION__);
    predicateSwitch = 1;
    self.fetchedResultsController = nil; 
    [self viewDidLoad];
}