Reloading UITableView based on settings in an iOS app
I'm trying to get a UITableView
to be dependent upon an in app setting. Based on the value of the setting, the UITableView
(in the RootViewController) would display the appropriate number of cells in the table. I check the setting (which is set by another ViewController and determine the cells to be displayed in the viewWillAppear
method of the RootViewController. Here's the code:
- (void)viewWillAppear:(BOOL)animated
{
if (menuList != nil) {
[menuList dealloc];
//[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
coreViewController.RootView = self;
//[self.tableView reloadData];
[[UIApplication sharedApplication] delegate];
self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
MyAppAppDelegate *appDelegate = (MyAppAppDelegate *)[[UIApplication sharedApplication] delegate];
menuList = [[NSMutableArray alloc] init];
[menuList addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"App Setup", @"App Setup"),kSelectKey, NSLocalizedString(@"Setup App Details",@"App Setup2"),kDescriptKey, nil, kControllerKey,nil]];
[menuList addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Selection 1", @"Selection 1"),kSelectKey, NSLocalizedString(@"Selection 1",@"Selection 1"),kDescriptKey, nil, kControllerKey,nil]];
[menuList addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Selection 2", @"Selection 2"),kSelectKey, NSLocalizedString(@"Selection 2",@"Selection 2"),kDescriptKey, nil, kControllerKey,nil]];
if (appDelegate.Level == 1) {
[menuList addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Selection 3", @"Selection 3"),kSelectKey, NSLocalizedString(@"Selection 3",@"Selection 3"),kDescriptKey, nil, kControllerKey,nil]];
}
if (appDelegate.Level == 2) {
[menuList addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Selection 3", @"Selection 3"),kSelectKey, NSLocalizedString(@"Selection 3",@"Selection 3"),kDescriptKey, nil, kControllerKey,nil]];
[menuList addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Selection 4", @"Selection 4"),kSelectKey, NSLocalizedString(@"Selection 4",@"Selection 4"),kDescriptKey, nil, kControllerKey,nil]];
}
if (appDelegate.Level == 3) {
[menuList addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Selection 3", @"Selection 3"),kSelectKey, NSLocalizedString(@"Selection 3",@"Selection 3"),kDescriptKey, nil, kControllerKey,nil]];
[menuList addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Selection 4", @"Selection 4"),kSelectKey, NSLocalizedString(@"Selection 4",@"Selection 4"),kDescriptKey, nil, kControllerKey,nil]];
[menuList addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Selection 5", @"Selection 5"),kSelectKey, NSLocalizedString(@"Selection 5",@"Selection 5"),kDescriptKey, nil, kControllerKey,nil]];
}
//add the final selection so that it is always last in the list
[menuList add开发者_StackOverflowObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Final Selection", @"Final Selection"),kSelectKey, NSLocalizedString(@"Final Selection",@"Final Selection"),kDescriptKey, nil, kControllerKey,nil]];
//[self.tableView reloadRowsAtIndexPaths:menuList withRowAnimation:true];
//[appDelegate.lastView replaceObjectAtIndex:0 withObject:[NSNumber numberWithInteger:-1]];
//[appDelegate.lastView replaceObjectAtIndex:1 withObject:[NSNumber numberWithInteger:-1]];
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
//[super viewWillAppear:animated];
}
As you can see, I've tried some various things to get the table to redisplay (I've left those commented in). When the code runs, the table is recreated, but the cells for selection 3 are not updated. (I want the cells to only reflect the selections available based on the level selected within the app)
note: I've tried using [self.tableView reloadData];
and it doesn't reload the third cell in the table. I forgot to include that info. What happens is upon the initial load the table looks like this:
Settings >
Selection 1>
Selection 2>
Final Selection>
When I change the level the table looks like:
Settings >
Selection 1>
Selection 2>
Final Selection>
Selection 4>
Final Selection>
This occurs no matter how I reload the table (even using reloadData).
I'm sure that I am missing something very obvious, and I appreciate any help given.
Why not just say [self.tableView reloadData]
?
That should do it?
what's wrong with [self.tableView reloadData]
?
and, if you want your [self.tableView reloadRowsAtIndexPaths:menuList withRowAnimation:true];
to work, you should give it an array with indexPathes not the array containing all the data.
精彩评论