2016-11-25 54 views
0

我正在为应用程序的黑暗模式工作。有一个切换的基本前提允许用户在黑暗模式和光照模式之间切换。全局更新全部uitableviewcell

我试图找出更新所有UITableViewCell's的最佳途径。 IE在'光照模式'下,细胞的背景颜色是白色的,在黑暗中的细胞背景颜色是黑色的。

目前我使用

- (void) tableView: (UITableView *) tableView 
    willDisplayCell: (UITableViewCell *) cell 
forRowAtIndexPath: (NSIndexPath *) indexPath 
{ 
    if(darkMode) 
    { 
     cell.backgroundColor  = darkBackground; 
     cell.textLabel.textColor = darkTextColor; 
    } 
    else 
    { 
     cell.backgroundColor  = lightBackground; 
     cell.textLabel.textColor = lightTextColor; 
    } 
} // End of tableView:willDisplayCell:forRowAtIndexPath: 

完成这里的问题是,当从暗/灯光用户切换我要重新绘制所有单元格,这样的背景被更新。我可以通过调用个人uitableviewreloaddata在每个表格基础上做到这一点,但我希望已在应用程序中加载EVERY TABLEVIEW以重新加载。这可能吗?

+0

挖成'UIAppearance'类 –

+0

用'UIAppearance'是标签的问题。例如,单元格可能包含textLabel和detailLabel,它们通常不应该是相同的颜色。我不认为UIA外观可以帮助解决这个问题。 – Kyle

+0

您可以创建符合UIAppearance的UITableViewCell的自己的子类,并使用UI_APPEARANCE_SELECTOR标记自定义设置器。 –

回答

0

我目前的解决办法是调酒的UITableView的,并添加一个通知处理程序。我不积极,如果这将是我最后的实施,因为我仍在调查。快速的代码看起来是这样的:

#import <JRSwizzle.h> 

@implementation UITableView (Swizzle) 

+ (void) load 
{ 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     NSError *error = nil; 

     const SEL deallocSel = NSSelectorFromString(@"dealloc"); 

     BOOL result = 
      [[self class] jr_swizzleMethod: @selector(initWithFrame:style:) 
           withMethod: @selector(initSQLProWithFrame:style:) 
            error: &error]; 

     if (!result || error) 
     { 
      NSLog(@"Can't swizzle methods - %@", [error description]); 
     } 

     result = 
      [[self class] jr_swizzleMethod: @selector(initWithCoder:) 
           withMethod: @selector(initSQLProWithCoder:) 
            error: &error]; 

     if (!result || error) 
     { 
      NSLog(@"Can't swizzle methods - %@", [error description]); 
     } 

     result = 
      [[self class] jr_swizzleMethod: deallocSel 
           withMethod: @selector(SQLProDealloc) 
            error: &error]; 

     if (!result || error) 
     { 
      NSLog(@"Can't swizzle methods - %@", [error description]); 
     } 
    }); 
} 

- (instancetype) initSQLProWithFrame: (CGRect)frame 
           style: (UITableViewStyle) style 
{ 
    self = [self initSQLProWithFrame: frame 
           style: style]; 

    if(self) 
    { 
     NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; 

     [nc addObserver: self 
       selector: @selector(themeChanged) 
        name: [SQLProAppearanceManager apperanceUpdatedNotificationName] 
       object: nil]; 

    } // End of self 

    return self; 
} // End of initSQLProWithFrame:style: 

- (instancetype) initSQLProWithCoder: (NSCoder*) coder 
{ 
    self = [self initSQLProWithCoder: coder]; 

    if(self) 
    { 
     NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; 

     [nc addObserver: self 
       selector: @selector(themeChanged) 
        name: [SQLProAppearanceManager apperanceUpdatedNotificationName] 
       object: nil]; 
    } // End of self 

    return self; 
} // End of initSQLProWithCoder: 

- (void) themeChanged 
{ 
    [self reloadData]; 
} // End of themeChanged 

- (void) SQLProDealloc 
{ 
    NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; 

    [nc removeObserver: self 
        name: [SQLProAppearanceManager apperanceUpdatedNotificationName] 
       object: nil]; 

    [self SQLProDealloc]; 
} // End of SQLProDealloc 

@end 
0

可以在viewWillAppear方法与UITableView每个控制器检查darkMode状态,并在必要时

+0

'UITableView'不支持'viewWillAppear'。 – Kyle

+0

这是视图控制器的方法 –

+0

是的,正确的,但我没有使用UITableViewController。只是一个UITableView。这两者不仅仅是可以互换的。 – Kyle