【问题标题】:How To Reload Table?如何重新加载表?
【发布时间】:2011-06-01 23:27:29
【问题描述】:

我们正在处理 2 个视图。主视图和子视图。

主视图的表格单元格的字符串显示该单元格的子视图中的对象数。当我从子视图中删除一个单元格并返回到主视图时,显示计数的字符串应该减少 1。现在这个字符串在我重新启动应用程序之前不会改变。

有什么办法可以解决这个问题吗?计数是从 NSFetch 中提取的。

编辑:添加代码

- (void)configureCell:(TDBadgedCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSDate *today = [NSDate date]; 
    NSDate *thisWeek  = [today dateByAddingTimeInterval: -604800.0];
    NSDate *thisMonth = [today dateByAddingTimeInterval: -2629743.83];
    else if (indexPath.row == 2)
    {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        self.predicate = [NSPredicate predicateWithFormat:@"(timeStamp >= %@) AND (timeStamp <= %@)", thisWeek, today];
        [fetchRequest setPredicate: predicate];
        [fetchRequest setEntity:[NSEntityDescription entityForName:@"Session" inManagedObjectContext:managedObjectContext]];
        NSError *error = nil;
        NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
        NSLog(@"Fetch error: %@", error);

        cell.badgeString = [NSString stringWithFormat:@"%i", [results count]];
        [fetchRequest release];
    }
    else if (indexPath.row == 3)
    {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        self.predicate = [NSPredicate predicateWithFormat:@"(timeStamp >= %@) AND (timeStamp <= %@)", thisMonth, today];
        [fetchRequest setPredicate:predicate];
        [fetchRequest setEntity:[NSEntityDescription entityForName:@"Session" inManagedObjectContext:managedObjectContext]];

        NSError *error = nil;
        NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
        NSLog(@"Fetch error: %@", error);

        cell.badgeString = [NSString stringWithFormat:@"%i", [results count]];
        [fetchRequest release];
    }
    else if (indexPath.row == 4)
    {
        cell.badgeString = [NSString stringWithFormat:@"%i", [[self.fetchedResultsController fetchedObjects] count]];
    }

【问题讨论】:

    标签: iphone objective-c uitableview


    【解决方案1】:

    如果您的数据实际上在模型方面得到了正确更新,您应该可以调用

    [tableView reloadData];
    

    一切都会正确更新。您可能可以将其放在 viewWillAppear 中。

    希望这会有所帮助。

    【讨论】:

    • 我试过了,但没有运气。当我重新启动应用程序时,它确实会正确更新,所以我不确定发生了什么。
    • 如果重要的话,我要更新的字符串是 TDBadgeCell 字符串属性 (github.com/tmdvs/TDBadgedCell)。
    • 当您的应用程序退出或退出活动应用程序时,是否会保存一些数据?您是否在更新模型后检查了模型,以查看数据是否已转移到您的主视图中?
    • 我很确定它可以正确保存,因为当我从主视图中选择给定行时,子表具有正确的数据,具体取决于我是否添加/删除了单元格。它只是在我重新启动应用程序之前不会更新的 mainView 计数。
    • 我会在你的主视图中设置一个断点,它会在其中获取你的数据容器的计数,并查看它是否正在更新甚至被调用。
    【解决方案2】:

    您需要确保您在 tableview 上获得了正确的句柄,如果您在头文件中为您的 tableview 创建了一个访问器,那么您需要在 viewWillLoad 方法中使用该名称调用它。

    【讨论】:

      猜你喜欢
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 2019-03-03
      • 2010-10-27
      • 1970-01-01
      • 2011-04-30
      相关资源
      最近更新 更多