谓词未正确过滤数据

问题描述

| 我正在尝试在单元格的badgeString中显示来自提取控制器的对象数。 由于某种原因,徽章对于所有单元格都保持不变,而不是针对每个单元格进行调整。
Indexpath.row ==2
是上周,
Indexpath.row == 3
是上个月。 在我的单元配置中,我有
if (indexPath.row > 1)
{
    cell.badgeString = [Nsstring stringWithFormat:@\"%i\",[[self.fetchedResultsController fetchedobjects]count]];
}
这是我的提取控制器方法
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@\"Session\" inManagedobjectContext:self.managedobjectContext];
[fetchRequest setEntity:entity];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0] ;
NSDate *today = [NSDate date]; 
NSDate *thisWeek  = [today dateByAddingTimeInterval: -604800.0];
NSDate *thisMonth = [today dateByAddingTimeInterval: -2629743.83]; // Use NSCalendar for

if (indexPath.row ==2)
{
    [fetchRequest setPredicate:[nspredicate predicateWithFormat:@\"(date >= %@) AND (date <= %@)\",thisWeek,today]];
}
else if (indexPath.row ==3)
{
    [fetchRequest setPredicate:[nspredicate predicateWithFormat:@\"(date >= %@) AND (date <= %@)\",thisMonth,today]];
}
全取控制器代码
- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil)
    {
        return __fetchedResultsController;
    }

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@\"Session\" inManagedobjectContext:self.managedobjectContext];
    [fetchRequest setEntity:entity];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0] ;
    NSDate *today = [NSDate date]; 
    NSDate *thisWeek  = [today dateByAddingTimeInterval: -604800.0];
    NSDate *thisMonth = [today dateByAddingTimeInterval: -2629743.83]; // Use NSCalendar for

    if (indexPath.row ==2)
    {
        [fetchRequest setPredicate:[nspredicate predicateWithFormat:@\"(date >= %@) AND (date <= %@)\",today]];
    }
    else if (indexPath.row ==3)
    {
        [fetchRequest setPredicate:[nspredicate predicateWithFormat:@\"(date >= %@) AND (date <= %@)\",today]];
    }

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@\"timeStamp\" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means \"no sections\".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedobjectContext:self.managedobjectContext sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error])
    {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application,although it may be useful during development. If it is not possible to recover from the error,display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@\"Unresolved error %@,%@\",error,[error userInfo]);
        abort();
    }
    NSLog(@\"Number of Objects = %i\",[[__fetchedResultsController fetchedobjects] count]);
    return __fetchedResultsController;
    NSLog(@\"Number of Objects = %i\",[[__fetchedResultsController fetchedobjects] count]);

}
单元配置代码
- (void)configureCell:(TDBadgedCell *)cell atIndexPath:(NSIndexPath *)indexPath
    {
    if (indexPath.row > 1)
        {
            cell.badgeString = [Nsstring stringWithFormat:@\"%i\",[[self.fetchedResultsController fetchedobjects]count]];
        }
    

解决方法

        
- (void) refreshTableData {
NSMutableArray *results = [NSMutableArray arrayWithCapacity:2];
...
NSArray *fetchResults = [first predicate call goes here];
[results addObject:fetchResults];
fetchResults = [second predicate call goes here];
[results addObject:fetchResults];
...
[tableview reloadData];
}
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...