我使用WWDC#2010中的TableViewUpdates示例。基本上,Apple通過單擊節標題創建可摺疊和可展開的TableView。對於TableView中得到的數據在viewWillAppear中創建像這樣:使用UINavigationController緩存數據
- (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時加載速度更快。謝謝。