草庐IT

ios - UITableView 背景色与 UITableViewCell 不同

coder 2024-01-28 原文

我将 tableView 背景颜色设置为与 tableViewCell 背景颜色相同,但颜色看起来不同。当设置为自定义颜色时,颜色看起来相同,但当没有自定义颜色设置时,它们是不同的。 isColorSet 在开始时为 0,因此两者都应设置为 UIColor(red: 74/255, green: 187/255, blue: 224/255, alpha: 1.0)。如何使颜色看起来相同?

设置tableView背景颜色:

override func viewWillAppear(_ animated: Bool) {
    //Set background color
    if userDefaults.integer(forKey: "isColorSet") == 0 {
        navigationController?.navigationBar.barTintColor = UIColor(red: 74/255, green: 187/255, blue: 224/255, alpha: 1.0)
        tableView.backgroundColor = UIColor(red: 74/255, green: 187/255, blue: 224/255, alpha: 1.0)

    } else if userDefaults.integer(forKey: "isColorSet") == 1 {
        let red = CGFloat(userDefaults.float(forKey: "red"))
        let green = CGFloat(userDefaults.float(forKey: "green"))
        let blue = CGFloat(userDefaults.float(forKey: "blue"))
        navigationController?.navigationBar.barTintColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1.0)
        tableView.backgroundColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1.0)
    }
}

设置tableViewCell背景颜色:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ideaCell") as! IdeaTableViewCell

    cell.ideaTitleLabel.text = ideas[indexPath.section][indexPath.row].title
    cell.ideaDescLabel.text = ideas[indexPath.section][indexPath.row].desc

    //Set date
    let date = userDefaults.object(forKey: "date.\(indexPath.section).\(indexPath.row)") as! Date
    let month = Calendar.current.component(.month, from: date)
    let day = Calendar.current.component(.day, from: date)
    let year = Calendar.current.component(.year, from: date)
    if userDefaults.string(forKey: "dateStyle") == "MDY" {
        cell.ideaDateLabel.text = "\(month)/\(day)/\(year)"
    } else if userDefaults.string(forKey: "dateStyle") == "DMY" {
        cell.ideaDateLabel.text = "\(day)/\(month)/\(year)"
    } else if userDefaults.string(forKey: "dateStyle") == "YMD" {
        cell.ideaDateLabel.text = "\(year)/\(month)/\(day)"
    }

    //Set color
    if userDefaults.integer(forKey: "isColorSet") == 0 {
        cell.backgroundColor = UIColor(red: 74/255, green: 187/225, blue: 224/225, alpha: 1.0)
    } else if userDefaults.integer(forKey: "isColorSet") == 1 {
        let red = CGFloat(userDefaults.float(forKey: "red"))
        let green = CGFloat(userDefaults.float(forKey: "green"))
        let blue = CGFloat(userDefaults.float(forKey: "blue"))
        cell.backgroundColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1.0)
    }

    return cell

为 UserDefaults 设置 rgb 颜色:

func HSBColorColorPickerTouched(sender: HSBColorPicker, color: UIColor, point: CGPoint, state: UIGestureRecognizer.State) {
    setButton.backgroundColor = color
    red = color.rgb()?.red
    green = color.rgb()?.green
    blue = color.rgb()?.blue
    redLabel.text = "Red: \(red!)"
    redSlider.value = Float(red)
    greenLabel.text = "Green: \(green!)"
    greenSlider.value = Float(green)
    blueLabel.text = "Blue: \(blue!)"
    blueSlider.value = Float(blue)
}

@IBAction func sliderValueChanged(_ sender: Any) {
    red = redSlider.value
    green = greenSlider.value
    blue = blueSlider.value
    if red != nil {
        redLabel.text = "Red: \(red!)"
    }
    if green != nil {
        greenLabel.text = "Green: \(green!)"
    }
    if blue != nil {
        blueLabel.text = "Blue: \(blue!)"
    }
    setButton.backgroundColor = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1.0)
}

@IBAction func setColor(_ sender: Any) {
    userDefaults.removeObject(forKey: "red")
    userDefaults.removeObject(forKey: "green")
    userDefaults.removeObject(forKey: "blue")
    userDefaults.set(red, forKey: "red")
    userDefaults.set(green, forKey: "green")
    userDefaults.set(blue, forKey: "blue")
    if userDefaults.float(forKey: "isColorSet") == 0 {
        userDefaults.set(1, forKey: "isColorSet")
    }
    _ = navigationController?.popToRootViewController(animated: true)
}

Screenshot with isColorSet = 0

Screenshot with isColorSet = 1

Screenshot of Color Picker

最佳答案

问题是以下行中的一个简单拼写错误:

cell.backgroundColor = UIColor(red: 74/255, green: 187/225, blue: 224/225, alpha: 1.0)

绿色和蓝色值的分母应为 255。

关于ios - UITableView 背景色与 UITableViewCell 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52709200/

有关ios - UITableView 背景色与 UITableViewCell 不同的更多相关文章

  1. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  2. ruby-on-rails - 使用 Rmagick 或 ImageMagick 在背景上放置标题 - 2

    我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. java - 为什么 ruby​​ modulo 与 java/other lang 不同? - 2

    我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.

  7. ruby-on-rails - 在 RSpec 中,如何以任意顺序期望具有不同参数的多条消息? - 2

    RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)

  8. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  9. ruby-on-rails - 如何用不同的用户运行nginx主进程 - 2

    A/ctohttp://wiki.nginx.org/CoreModule#usermaster进程曾经以root用户运行,是否可以以不同的用户运行nginxmaster进程? 最佳答案 只需以非root身份运行init脚本(即/etc/init.d/nginxstart),就可以用不同的用户运行nginxmaster进程。如果这真的是你想要做的,你将需要确保日志和pid目录(通常是/var/log/nginx&/var/run/nginx.pid)对该用户是可写的,并且您所有的listen调用都是针对大于1024的端口(因为绑定(

  10. ruby - 从 sinatra 中的 before do block 返回不同的值 - 2

    有没有办法在sinatra的beforedoblock中停止执行并返回不同的值?beforedo#codeishere#Iwouldliketo'return"Message"'#Iwouldlike"/home"tonotgetcalled.end//restofthecodeget'/home'doend 最佳答案 beforedohalt401,{'Content-Type'=>'text/plain'},'Message!'end如果你愿意,你可以只指定状态,这里有状态、标题和正文的例子

随机推荐