我想按“暗模式”后刷新tableView外观。但它看起来像这样: Before press"Dark mode" After press"Dark mode" 我改变它的外观后如何刷新这个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中)
答案 0 :(得分:2)
您似乎没有致电reloadData()
一旦您的样式更改结束,请致电执行以下代码
tableView.reloadData()
另一件事是,你的tableViewCell
样式应该在
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
不在setStyleMode()
函数内。您可以根据某些bool值更改样式。
希望这能解决您的问题
答案 1 :(得分:0)
您正在设置单元格的背景颜色,而不是单元格的contentView
背景颜色。
你应该这样做:
let cell = tableView.cellForRowAtIndexPath(cellPath)
cell?.backgroundColor = ....
cell?.contentView.backgroundColor = ....
如果您的单元格在层次结构中有更多的包装器视图,则可能需要深入挖掘。
但有几点意见:
UITableViewCell
子类中定义方法以定义暗和亮模式将是一种更好的模式。在复杂的单元结构中,您可能无法轻松访问所需的所有子视图。那么,封装呢?UITableViewDatasource
方法似乎是一种反模式。isDarkMode
答案 2 :(得分: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)