• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UITableView 背景颜色与 UITableViewCell 不同

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:40:27 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我将 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



Best Answer-推荐答案


问题是以下行中的一个简单错字:

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap