2016-08-14 173 views
0

我要刷新的tableView外观后,我按“黑暗模式”。但它看起来像这样: Before press"Dark mode" After press"Dark mode" 我怎么能刷新这个的tableView后,我改变了它的外观刷新的tableView外观

mycode的:

@IBAction func setDarkMode(sender: UISwitch) { 
    UIView.animateWithDuration(0.5, delay:0,options:UIViewAnimationOptions.BeginFromCurrentState, animations: {() -> Void in 
     self.setStyleMode() 
    }) { (finish: Bool) -> Void in 
    } 
} 
func setStyleMode() { 
    if isDarkMode { 
     view.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0) 
     self.tableView.backgroundView?.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0) 
     tableView.separatorColor = UIColor(red:0.3137, green:0.3137, blue:0.3137, alpha:1.0) 
     self.navigationController?.navigationBar.barTintColor = UIColor(red:0.1451, green:0.1451, blue:0.1451, alpha:1.0) 
     self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor(red:0.6549, green:0.6549, blue:0.6549, alpha:1.0)] 
     self.navigationController?.navigationBar.tintColor = UIColor(red:0.9412, green:0.3412, blue:0.302, alpha:1.0) 
     for sectionIndex in 0...tableView.numberOfSections - 1 { 
      for rowIndex in 0...tableView.numberOfRowsInSection(sectionIndex) - 1 { 
       let cellPath = NSIndexPath(forRow: rowIndex, inSection: sectionIndex) 
       let cell = tableView.cellForRowAtIndexPath(cellPath) 
       cell?.backgroundColor = UIColor(red:0.1451, green:0.1451, blue:0.1451, alpha:1.0) 
      } 
     } 
     for aLabel in labels { 
      aLabel.textColor = UIColor(red:0.6549, green:0.6549, blue:0.6549, alpha:1.0) 
     } 

    } else { 
     view.backgroundColor = UIColor.whiteColor() 
     tableView.backgroundColor = UIColor(red:0.9529, green:0.9529, blue:0.9529, alpha:1.0) 
     tableView.separatorColor = UIColor(red:0.7372, green:0.7371, blue:0.7372, alpha:1.0) 
     self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor() 
     self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor()] 
     self.navigationController?.navigationBar.tintColor = UIColor(red:0.9412, green:0.3412, blue:0.302, alpha:1.0) 
     for sectionIndex in 0...tableView.numberOfSections - 1 { 
      for rowIndex in 0...tableView.numberOfRowsInSection(sectionIndex) - 1 { 
       let cellPath = NSIndexPath(forRow: rowIndex, inSection: sectionIndex) 
       let cell = tableView.cellForRowAtIndexPath(cellPath) 
       cell?.backgroundColor = UIColor.whiteColor() 
       //do stuff with 'cell' 
      } 
     } 
     for aLabel in labels { 
      aLabel.textColor = UIColor.blackColor() 
     } 
    } 
} 

(此“设置”的tableView处于“tableviewController”,并在ContainerView嵌入)

+1

你是如何控制/提供章节标题?你只是没有对他们做任何事情。 – Wain

+0

也,你有没有确切的说您不满意所以大家在这里猜测是哪一部分? – Wain

回答

2

看起来你已经不叫reloadData()

一旦你的风格变化超过召呼执行以下代码

tableView.reloadData()

另一件事是,你的造型应该tableViewCell在里面

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

未完成setStyleMode()功能。你可以根据一些布尔值来改变样式。

希望这将解决您的问题

+0

没关系索引路径编辑的细胞外排,你并不需要重新加载表在这种情况下 – Wain

+1

是啊,这是正确的,但 “为的rowIndex在0 ... tableView.numberOfRowsInSectio”是每一个细胞是很多比什么是绰绰有余。如果细胞的数量少,外面编辑细胞是确定 – renjithr

+0

这是一个猜测,但我期待的表格视图是细胞在这种情况下,静态列表 – Wain

0

你设置单元格的背景颜色,但不是细胞的contentView的背景色。

你应该这样做:

let cell = tableView.cellForRowAtIndexPath(cellPath) 
cell?.backgroundColor = .... 
cell?.contentView.backgroundColor = .... 

你可能有,如果您的电池已在层次结构的详细包装的意见进行深入分析。

几个观察,虽然:

  1. 这将是更好的图案,以限定UITableViewCell子类中的一个方法,以限定一个深色和浅色模式。在复杂的单元结构中,您可能无法轻松访问所需的所有子视图。另外,封装呢?
  2. 我也更喜欢重载方式。手动调用UITableViewDatasource方法看起来像是反模式。
  3. 有一个在你的代码有很多重复的。你可以显著减少的依赖,例如,使用三元运营商对isDarkMode
0

需要改变“headerView”和“footerView”背景“setStyleMode()”

let header = tableView.headerViewForSection(sectionIndex) 
let footer = tableView.footerViewForSection(sectionIndex) 
header?.contentView.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0) 
footer?.contentView.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0)