Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView背景色与UITableViewCell不同_Ios_Swift_Uitableview_Tableview - Fatal编程技术网

Ios UITableView背景色与UITableViewCell不同

Ios UITableView背景色与UITableViewCell不同,ios,swift,uitableview,tableview,Ios,Swift,Uitableview,Tableview,我将tableView背景色设置为与tableViewCell背景色相同,但颜色看起来不同。设置为自定义颜色时,颜色看起来相同,但没有自定义颜色集时,颜色不同。isColorSet在开始时为0,因此两者都应设置为UIColor(红色:74/255,绿色:187/255,蓝色:224/255,alpha:1.0)。我怎样才能让颜色看起来一样 设置tableView背景色: override func viewWillAppear(_ animated: Bool) { //Set back

我将tableView背景色设置为与tableViewCell背景色相同,但颜色看起来不同。设置为自定义颜色时,颜色看起来相同,但没有自定义颜色集时,颜色不同。isColorSet在开始时为0,因此两者都应设置为
UIColor(红色:74/255,绿色:187/255,蓝色: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)
    }
}
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
设置tableViewCell背景色:

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)
    }
}
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
为用户默认设置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)
}


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

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

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

没有定制颜色时,它们是什么颜色?如果可以的话,请包括一个屏幕截图。@toddg我已经添加了一个屏幕截图链接,因为我没有足够的声誉来嵌入图片。好的,明白了。问题是你得到了两个蓝色的阴影,还是当细胞应该是蓝色的时候它是白色的?问题是我得到了两个蓝色的阴影。我将用iColorSet为1时的屏幕截图更新该问题。似乎在序列化/反序列化到
userDefaults
时可能会出现问题。你能在那里张贴设置这些值的代码吗?在代码中插入一些print语句可能会有所帮助,以查看当
isColorSet=0
噢,哇,对不起,在问为什么会发生这种情况之前,我没听清楚。感谢发生在我们身上:-)