刷新tableView外观

问题描述 投票:0回答:3

我想在按下“深色模式”后刷新 tableView 外观。但它看起来像这样:

按“深色模式”之前

Before press

按“深色模式”后

After press 更改外观后如何刷新此 tableView

我的代码:

@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中)

ios swift uitableview
3个回答
3
投票

看起来你还没有打电话

reloadData()

一旦您的样式更改结束,请执行以下代码

tableView.reloadData()

另一件事是,你的

tableViewCell
的造型应该在

内部完成
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

不在

setStyleMode()
函数内。您可以根据某些布尔值更改样式。

希望这能解决您的问题


0
投票

您正在设置单元格的背景颜色,而不是单元格

contentView
的背景颜色。

你应该这样做:

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

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

但有一些观察结果:

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

0
投票

需要在“setStyleMode()”中更改“headerView”和“footerView”的背景

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)
© www.soinside.com 2019 - 2024. All rights reserved.