点击时 UITableViewCells 不会改变颜色

Posted

技术标签:

【中文标题】点击时 UITableViewCells 不会改变颜色【英文标题】:UITableViewCells won't change color when tapped on 【发布时间】:2019-06-25 22:30:07 【问题描述】:

我正在尝试在我的应用程序中创建“更改主题”选项。出于某种原因,这段代码总是会以某种方式实现,这样当我点击单元格时,例如,如果我在我的应用程序当前为浅色主题时点击“深色主题”,我的所有代码都会触发并且导航栏变为深灰色,我的 tableview 背景变成了它应该是的颜色,等等,但是“Light Theme”保持在 Light 模式。除非我返回设置视图控制器并重新输入“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

【问题讨论】:

【参考方案1】:

不要从 didSeleteRowAtIndexPath 更改 tableView 单元格属性。重新加载 tableView 并将 cellDesign 的条件放在 cellForRowAtIndexPath 中。下面是一个建议-

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

NSInteger theme = [[NSUserDefaults standardUserDefaults] integerForKey:@"theme"];
    if (indexPath.row == 0) 
       theme = 0;
    else if (indexPath.row ==1)
       theme = 1;
    
     [[NSUserDefaults standardUserDefaults] setInteger:theme forKey:@"theme"];
[[NSUserDefaults standardUserDefaults] synchronize];
    [self changeTheme:theme];
     [tableView reloadData];
 
-(void)changeTheme:(NSInteger)theme
   if(theme==0)
     // do necessary changes for theme =0 for View Controller except cells
   else
     // do necessary changes for theme =1 for View Controller except cells
   

【讨论】:

以上是关于点击时 UITableViewCells 不会改变颜色的主要内容,如果未能解决你的问题,请参考以下文章

回收时不会重新布局自定义 iOS UITableViewCells

Swift:点击一个单元格时更改所有 UITableViewCells 的文本颜色

点击下一个/完成时在不同的 UITableViewCells 中导航 UITextFields

如何防止自定义 UITableViewCells 在取消选择时闪烁白色?

UITextfield在UITableViewCells中 - 关闭键盘

标记有时会在点击时改变颜色,有时不会