我正在尝试在应用程序中设置“更改主题”选项。由于某种原因,此代码将始终以某种方式实现,例如,当我点击单元格时(例如,当我当前是浅色主题时,在我的应用中点击“黑暗主题”时),我的所有代码都会触发并且导航栏变为深灰色,我的tableview背景变成了应该的颜色,等等,但是“ Light Theme”仍然处于灯光模式。除非我回到设置viewcontroller并重新输入“ ChangeThemeViewController”,否则它将仍然保留白色背景和黑色文本。我很困惑,我一直在努力解决这个问题。任何人都知道为什么这样做以及如何解决它?这是我的代码。
#import "ChangeThemeViewController.h"
@interface ChangeThemeViewController ()
@end
@implementation ChangeThemeViewController {
NSArray *tableData;
NSInteger theme;
NSString *simpleTableIdentifier;
UITableViewCell *cell;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSInteger theme = [[NSUserDefaults standardUserDefaults]
integerForKey:@"theme"];
[[NSUserDefaults standardUserDefaults] setInteger:theme forKey:@"theme"];
[[NSUserDefaults standardUserDefaults] synchronize];
//add variable for theme here
// if (theme == 0) {
// self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//
// self.navigationController.navigationBar.barTintColor = nil;
// _tableView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
// [self.navigationController.navigationBar
// setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
// }
//
//
// if (theme == 1) {
// self.tableView.backgroundColor = [UIColor blackColor];
//
// self.navigationController.navigationBar.barTintColor = [UIColor darkGrayColor];
// _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
// [self.navigationController.navigationBar
// setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
// }
tableData = [NSArray arrayWithObjects:@"Light Theme", @"Dark Theme", nil];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
simpleTableIdentifier = @"SimpleTableItem";
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
//add variable for theme here
NSInteger theme = [[NSUserDefaults standardUserDefaults]
integerForKey:@"theme"];
if (theme == 0) {
NSLog(@"theme is 0, running cellforrowindexpath");
cell.textLabel.textColor = [UIColor blackColor];
cell.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.tintColor = nil;
tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
if (theme == 1) {
NSLog(@"theme is 1, running cellforrowindexpath");
UIColor *color = [UIColor colorWithRed:30/255.0
green:30/255.0
blue:30/255.0
alpha:1];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor blackColor];
tableView.backgroundColor = color;
}
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = [tableData objectAtIndex:0];
}
if (indexPath.row == 1) {
cell.textLabel.text = [tableData objectAtIndex:1];
}
}
}
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[NSUserDefaults standardUserDefaults] setInteger:theme forKey:@"theme"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSInteger theme = [[NSUserDefaults standardUserDefaults] integerForKey:@"theme"];
if (indexPath.row == 0) {
[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
NSLog(@"row 0 picked should change into light mode");
theme = 0;
[[NSUserDefaults standardUserDefaults] setInteger:theme forKey:@"theme"];
[[NSUserDefaults standardUserDefaults] synchronize];
self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.navigationController.navigationBar.barTintColor = nil;
_tableView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
[self.navigationController.navigationBar
setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
self.navigationController.navigationBar.tintColor = nil;
}
if (indexPath.row == 1) {
[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
NSLog(@"row 1 picked should change into dark mode");
theme = 1;
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[[NSUserDefaults standardUserDefaults] setInteger:theme forKey:@"theme"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIColor *color = [UIColor colorWithRed:30/255.0
green:30/255.0
blue:30/255.0
alpha:1];
self.tableView.backgroundColor = color;
self.navigationController.navigationBar.barTintColor = [UIColor darkGrayColor];
_tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[self.navigationController.navigationBar
setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
}
if (theme == 0) {
cell.backgroundColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor blackColor];
}
if (theme == 1) {
cell.backgroundColor = [UIColor blackColor];
cell.textLabel.textColor = [UIColor whiteColor];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
@end
答案 0 :(得分:0)
请勿从didSeleteRowAtIndexPath中更改tableView单元格属性。重新加载tableView并将cellDesign的条件放入cellForRowAtIndexPath中。以下是建议-
{{1}}