刷新tableView外观

时间:2016-08-14 07:23:36

标签: ios swift uitableview

我想按“暗模式”后刷新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中)

3 个答案:

答案 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 = ....

如果您的单元格在层次结构中有更多的包装器视图,则可能需要深入挖掘。

但有几点意见:

  1. UITableViewCell子类中定义方法以定义暗和亮模式将是一种更好的模式。在复杂的单元结构中,您可能无法轻松访问所需的所有子视图。那么,封装呢?
  2. 我也更喜欢重装方式。手动调用UITableViewDatasource方法似乎是一种反模式。
  3. 您的代码中存在大量重复内容。例如,您可以使用isDarkMode
  4. 上的三元运算符来显着降低依赖性

答案 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)
相关问题