我有一個應用程序組成TabBar與幾個TabBarControllers。一個控制器包含一個非常簡單的表格,它應該顯示一個NSMutableDictionary的內容。當您點擊相應的按鈕時,字典將在單獨的控制器中更新,並且視圖切換到UITableViewController
,顯示新更新的表格。UITableView不刷新
我可以看到正在更新的字典。但是TableView從不反映這些變化。實際上,它似乎只在我第一次進入該屏幕時顯示更改。
我已經嘗試過[self table.reloadData],而它被調用時,更改不會反映到UITableView
。
有沒有人有任何建議?我很高興發佈代碼,但我不確定發佈什麼。
更新:僅在第一次顯示錶格時更新並刷新表格。隨後的顯示器只顯示原始內容。
背景: tableview從字典中獲取:appDelegate.currentFave。每次TabBarController調用ViewController時,tableview都會刷新。
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"in viewWillAppear");
[super viewWillAppear:animated];
[self loadFavesFile];
[self.tableView reloadData];
}
// load the Favorites file from disk
- (void) loadFavesFile
{
// get location of file
NSString *path = [self getFavesFilePath];
// The Favorites .plist data is different from the Affirmations in that it will never be stored in the bundle. Instead,
// if it exists, then use it. If not, no problem.
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
// read Faves file and store it for later use...
NSMutableDictionary *tempDict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
appDelegate.sharedData.dictFaves = tempDict;
// grab the latest quote. Append it to the list of existing favorites
NSString *key = [NSString stringWithFormat:@"%d", appDelegate.sharedData.dictFaves.count + 1];
NSString *newFave = [NSString stringWithFormat:@"%@", appDelegate.currentFave];
[appDelegate.sharedData.dictFaves setObject:newFave forKey:key];
} else {
NSLog(@"Favorites file doesn't exist");
appDelegate.sharedData.dictFaves = nil;
}
}
// this gets invoked the very first call. Only once per running of the App.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// reuse or create the cell
static NSString *cellID = @"cellId";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
// allow longer lines to wrap
cell.textLabel.numberOfLines = 0; // Multiline
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.font = [UIFont fontWithName:@"Chalkduster" size:(16)];
cell.textLabel.textColor = [UIColor yellowColor];
// NOTE: for reasons unknown, I cannot set either the cell- or table- background color. So it must be done using the Label.
// set the text for the cell
NSString *row = [NSString stringWithFormat:@"%d", indexPath.row + 1];
cell.textLabel.text = [appDelegate.sharedData.dictFaves objectForKey:row];
return cell;
}
我的部分錯別字。道歉。 – Bassman 2012-02-02 21:29:06
這應該是一個評論,而不是答案 – 2012-02-11 23:06:44