我有一个应用程序组成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