2016-08-14 170 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)