简体   繁体   English

UITableView背景色与UITableViewCell不同

[英]UITableView Background Color Different From UITableViewCell

I am setting the tableView background color the same as the tableViewCell background color, but the colors look different. 我将tableView背景色设置为与tableViewCell背景色相同,但是颜色看起来不同。 The colors look the same when set to a custom color but when there is no custom color set they are different. 设置为自定义颜色时,颜色看起来相同,但是当未设置自定义颜色时,颜色则不同。 isColorSet is 0 on start so both should be set to UIColor(red: 74/255, green: 187/255, blue: 224/255, alpha: 1.0) . isColorSet在开始时为0,因此两者都应设置为UIColor(red: 74/255, green: 187/255, blue: 224/255, alpha: 1.0) How can I get the colors to look the same? 如何获得相同的颜色?

Setting tableView background color: 设置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)
    }
}

Setting tableViewCell background color: 设置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

Setting rgb colors for UserDefaults: 为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 isColorSet = 0的屏幕截图

Screenshot with isColorSet = 1 isColorSet = 1的屏幕截图

Screenshot of Color Picker 拾色器的屏幕截图

The issue is a simple typo in the following line: 问题是以下行中的一个简单错字:

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

Your denominator should be 255 for the green and blue values. 绿色和蓝色值的分母应为255。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM