【问题标题】:UITableView Background Color Different From UITableViewCellUITableView 背景颜色与 UITableViewCell 不同
【发布时间】:2019-03-13 12:48:48
【问题描述】:

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

【问题讨论】:

  • 没有自定义颜色时它们是什么颜色?如果可以,请附上屏幕截图。
  • @toddg 我已经添加了截图的链接,因为我还没有足够的声望来嵌入图片。
  • 好的,知道了。问题是你得到了两种蓝色还是单元格应该是蓝色的时候是白色的?
  • 问题是我得到了两种蓝色。我将用 isColorSet 何时为 1 的屏幕截图更新问题。
  • 看来序列化/反序列化到userDefaults 可能存在问题。您可以在此处发布设置这些值的代码吗?在该代码中插入一些打印语句以查看isColorSet = 0 时您设置的 rgb 值可能会有所帮助

标签: ios swift uitableview tableview


【解决方案1】:

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

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

绿色和蓝色值的分母应该是 255。

【讨论】:

  • 哦,抱歉,在问为什么会这样之前我没听清楚。谢谢
  • 这发生在我们所有人身上:-)
猜你喜欢
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
相关资源
最近更新 更多