У меня есть приложение, состоящее из TabBar с несколькими TabBarControllers. Один контроллер содержит очень простую таблицу, которая должна отображать содержимое NSMutableDictionary. Когда вы нажимаете соответствующую кнопку, словарь обновляется в отдельном контроллере, и представление переключается на UITableViewController
, отображая обновленную таблицу.
Я вижу, что словарь обновляется. Но TableView никогда не отражает изменения. На самом деле, кажется, что изменения отображаются только при первом входе на этот экран.
Я попытался [self table.reloadData], и пока он вызывается, изменения не отражаются в UITableView
.
У кого-нибудь есть предложения? Я рад опубликовать код, но не уверен, что писать.
Обновление: таблица обновляется и обновляется должным образом только в первый раз, когда она отображается. Последующие дисплеи просто показывают оригинальное содержание.
Справочная информация:
Табличное представление заполняется из словаря: appDelegate.currentFave. Табличное представление должно обновляться каждый раз, когда ViewBontroller вызывается TabBarController.
- (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;
}