【问题标题】:Caching data using UINavigationController使用 UINavigationController 缓存数据
【发布时间】:2012-01-05 00:08:01
【问题描述】:

我正在使用 WWDC #2010 中的 TableViewUpdates 示例。基本上,Apple 通过单击部分标题来创建可折叠和可展开的 TableView。 TableView 的数据在 viewWillAppear 中创建,如下所示:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated]; 

    /*
     Check whether the section info array has been created, and if so whether the section count still matches the current section count. In general, you need to keep the section info synchronized with the rows and section. If you support editing in the table view, you need to appropriately update the section info during editing operations.
     */
    if ((self.sectionInfoArray == nil) || ([self.sectionInfoArray count] != [self numberOfSectionsInTableView:self.tableView])) {

        // For each play, set up a corresponding SectionInfo object to contain the default height for each row.
        NSMutableArray *infoArray = [[NSMutableArray alloc] init];

        for (Play *play in self.plays) {

            SectionInfo *sectionInfo = [[SectionInfo alloc] init];          
            sectionInfo.play = play;
            sectionInfo.open = NO;

            NSNumber *defaultRowHeight = [NSNumber numberWithInteger:DEFAULT_ROW_HEIGHT];
            NSInteger countOfQuotations = [[sectionInfo.play quotations] count];
            for (NSInteger i = 0; i < countOfQuotations; i++) {
                [sectionInfo insertObject:defaultRowHeight inRowHeightsAtIndex:i];
            }

            [infoArray addObject:sectionInfo];
            [sectionInfo release];
        }

        self.sectionInfoArray = infoArray;
        [infoArray release];
    }

}

我注意到我的案例中,我有大量数据,这是一项昂贵的操作。我想缓存数据。每次都会创建数据,因为它位于 viewWillAppear 中。因为我正在使用 UINavigationController 将此视图推送到堆栈上,如果我将其放入 viewDidLoad,当我离开此视图并返回家时,我必须再次重新创建视图,viewDidLoad 将再次运行,并且它会再慢一点。

我以前没有缓存过数据,想知道有什么好的方法吗?现在,行标题和行的所有数据都在数据库中。所以当这个视图被压入堆栈时,我会抓取数据并创建表。我不知道创建表并以某种方式缓存视图或其他东西以使其在 viewController 的后续推送时更快地加载的良好机制是什么。谢谢。

【问题讨论】:

    标签: iphone uitableview uinavigationcontroller


    【解决方案1】:

    您展示的代码是为表格视图构建数据源,本身并不是视图本身的一部分。在视图控制器初始化程序和/或数据源需要更新时执行此代码是否满足您的需求?

    您可以使用 NSFetchedResultsController 及其委托方法来绘制平行线。这些是与视图处理方法分开执行的,获取的结果控制器是 ivar 或(通常)是视图控制器的属性。然后,例如,一旦 fetched results controller 完成了它的 fetch,它可以根据具体情况管理更改,与 table view 及其控制器协调,或者您可以故意完全重新获取。视图可以完全独立于数据源维护而出现和消失。

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2018-12-03
      相关资源
      最近更新 更多