swift - 开关打开时更改 UITableViewCells 的背景颜色 - Swift

标签 swift xcode uitableview

我试图做到这一点,以便用户在打开暗模式时(使用开关)将表格 View 单元格的背景颜色更改为黑色(因此成为暗模式)。我还想知道如何在开关打开时更改导航栏的颜色。

下面是我试过的(完整代码):

import Foundation
import UIKit

class SideMenuController8: UITableViewController{

    @IBOutlet var TableViewColor: UITableView!

    @IBOutlet weak var OpenSettings: UIBarButtonItem!
    @IBOutlet weak var mSwitch: UISwitch!
    @IBOutlet weak var dSwitch: UISwitch!
    override func viewDidLoad() {

        self.navigationController?.navigationBar.topItem!.title = "Settings"


        if revealViewController() != nil {
            OpenSettings.target = revealViewController()
            OpenSettings.action = #selector(SWRevealViewController.revealToggle(_:))
            view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }




//        mSwitch.layer.borderWidth = 1
//        mSwitch.layer.borderColor = UIColor.white.cgColor
        let onColor  = UIColor(red: CGFloat(0.0), green: CGFloat(122.0 / 255.0), blue: CGFloat(1.0), alpha: CGFloat(1.0))
        let offColor = UIColor.white

        //Notifications On Switch
        mSwitch.isOn = false
        /*For on state*/
        mSwitch.onTintColor = onColor
        /*For off state*/
        mSwitch.tintColor = offColor
        mSwitch.layer.cornerRadius = 16
        mSwitch.backgroundColor = offColor

        //Dark Mode Switch
        dSwitch.isOn = false
        /*For on state*/
        dSwitch.onTintColor = onColor
        /*For off state*/
        dSwitch.tintColor = offColor
        dSwitch.layer.cornerRadius = 16
        dSwitch.backgroundColor = offColor




            if (dSwitch.isOn == true){

                TableViewColor.reloadData()
                print("Dark Mode Switch is on")

            }


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

Image

这是另一张展示我的主要 Storyboard的图片

enter image description here

最佳答案

对于 UITableViewCells 的背景颜色,您可以使用一个 bool 值来指示夜间模式是否开启,就像您现在所做的那样。

当开关值发生变化(从关闭到打开或反之亦然)时,您应该调用 tableView.reloadData()。这样,将为每个单元格再次调用方法 cellForIndexPath。在此方法中,您应该检查夜间模式是否打开(使用 bool 值),因此相应地设置单元格的背景颜色。

对于导航栏,您可以使用名为 barTintColor 的属性。您可以通过以下方式使用它

UINavigationController?.navigationBar.barTintColor = //your color

此外,请记住您应该实现 tableView 的数据源方法。由于您的 tableviewController 已经是 datasourcedelegate,您只需覆盖它们。有3个重要的。

//Number of sections
override func numberOfSections(in tableView: UITableView) -> Int

//Number of rows in a section
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

//In this one you setup or return a tableviewcell for the specific
//IndexPath. Here you should create a UITableViewCell and set its background
//color accordingly to the value of the switch
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

此方法是 UITableViewController 的基本方法。这可能超出了问题的范围,您可以在多个来源中查看更多信息。这是一个解释更多一点的例子https://www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/

关于swift - 开关打开时更改 UITableViewCells 的背景颜色 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42149029/

相关文章:

ios - 点击时更改 TableView 高度(在 xib 中使用自动布局)

swift - Material 示例在启动时崩溃

ios - 将字符串转换为 Int 数组

swift - 无法将类型 'NSAttributedString.DocumentAttributeKey' 的值转换为预期的字典键类型 'NSAttributedString.DocumentReadingOptionKey'

objective-c - 如何在 UITextField 中键入文本时更新 UILabel

swift - 如何根据相同的 id 添加两行 TableView

json - 序列化 JSON "keyNotFound"时出错

ios - 在 Swift/Objc 混合项目中关闭模态 Controller 触发的 Segue

javascript - iOS Phonegap 媒体对象 - 如何访问 WWW 文件夹中的资源?

ios - UISearchController 的 UISearchBar 在事件时移出屏幕