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

拉贾特·卡雷(Rajat Khare)

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

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

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.
    }

}

图像

这是另一个图片,显示了我的主要情节提要

在此处输入图片说明

费德里科·奥耶达(Federico Ojeda)

对于UITableViewCells的背景色,可以像现在一样使用布尔值指示夜间模式是否打开。

When there is a change in the switch value (from off to on or viceversa), you should call tableView.reloadData(). This way, the method cellForIndexPath will be called again for each one of the cell. In this method, you should check if the night mode is on (with the boolean) and therefore set the backgroundColor of the cell accordingly.

For the navigationBar, you could use the property called barTintColor. You can use it the following way

UINavigationController?.navigationBar.barTintColor = //your color

Also, remember that you should implement the datasource methods of the tableView. Since your tableviewController is already the datasource and delegate, you just have to override them. There are 3 important ones.

//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/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Swift:在点击一个单元格时更改所有UITableViewCells的文本颜色

Swift 3 - 通过开关(暗模式/夜间模式)更改所有视图控制器的背景颜色

iOS Swift-更改背景颜色

在Swift 2.0中更改NSView的背景颜色

在Swift中更改滑动删除背景的颜色

在Swift中逐渐更改背景颜色

在 swift UI 中点击按钮时如何更改背景颜色和图像?

如何在iOS Swift中更改背景视图颜色偏

在Swift 3中更改状态栏背景颜色

如何将UiSearchBar的背景颜色更改为黑色-SWIFT

Swift:UISegmentControl的选定段中的背景颜色更改

iOS Swift-如何更改表格视图的背景颜色?

更改ViewController Swift的背景颜色?(单视图应用程序)

Swift-将背景颜色更改为透明

Swift - 在 tableView.selectRow 中更改背景颜色

Swift中的UIView背景颜色

Swift 3 / IQKeyboardManager:如何更改工具栏的背景颜色,完成颜色,箭头颜色?

在 Swift iOS 中选择 collectionView Cell 时更改标签颜色

Swift-轻按UIButton时更改文本的颜色和大小

Swift TableView防止选择时更改按钮颜色

更改附件的颜色类型Swift

选择SWIFT时更改按钮背景色

更改自定义文本字段的背景颜色和文本颜色IOS Swift

如何在 swift 3 上将 UITableViewController 的背景颜色更改为渐变颜色

每次按下按钮时,Swift 都会使背景颜色闪烁或闪烁 2 种随机颜色

循环更改背景颜色与javascript中的开关

Swift - 顶部带有背景颜色的 tableView

Swift:从AppDelegate设置ViewController背景颜色

在Swift中为UIAlertController设置背景颜色