是否可以更改UIButton的谓词?

时间:2014-08-17 22:30:12

标签: objective-c core-data ios7 nspredicate

我在Core Data模型中有一组单词。我想有一些基于类别过滤的按钮,如形容词,动词等。

我有类似下面的代码,但表格没有重新加载..

> - (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 = .....;

我得到整个表,但是谓词没有触发,并且表视图没有重新加载。我很感激任何建议。

1 个答案:

答案 0 :(得分:0)

有些事情错了..

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

  2. 您不能只是重新加载表格。你必须重新加载视图控制器..(我不喜欢这样,所以我愿意接受替代方案)。

  3. 您不能只使用

    重置缓存

    [NSFetchedResultsController deleteCacheWithName:nil];

  4. 你必须锤击它:

    self.fetchedResultsController = nil;
    

    所以:

    - (IBAction)selectNouns:(id)sender {
        //NSLog(@"%s", __FUNCTION__);
        predicateSwitch = 1;
        self.fetchedResultsController = nil; 
        [self viewDidLoad];
    }