菜鸟教程小白 发表于 2022-12-11 20:40:27

ios - UITableView 背景颜色与 UITableViewCell 不同


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

<p>设置tableView背景颜色:</p>

<pre><code>override func viewWillAppear(_ animated: Bool) {
    //Set background color
    if userDefaults.integer(forKey: &#34;isColorSet&#34;) == 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: &#34;isColorSet&#34;) == 1 {
      let red = CGFloat(userDefaults.float(forKey: &#34;red&#34;))
      let green = CGFloat(userDefaults.float(forKey: &#34;green&#34;))
      let blue = CGFloat(userDefaults.float(forKey: &#34;blue&#34;))
      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)
    }
}
</code></pre>

<p>设置tableViewCell背景颜色:</p>

<pre><code>override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: &#34;ideaCell&#34;) as! IdeaTableViewCell

    cell.ideaTitleLabel.text = ideas.title
    cell.ideaDescLabel.text = ideas.desc

    //Set date
    let date = userDefaults.object(forKey: &#34;date.\(indexPath.section).\(indexPath.row)&#34;) 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: &#34;dateStyle&#34;) == &#34;MDY&#34; {
      cell.ideaDateLabel.text = &#34;\(month)/\(day)/\(year)&#34;
    } else if userDefaults.string(forKey: &#34;dateStyle&#34;) == &#34;DMY&#34; {
      cell.ideaDateLabel.text = &#34;\(day)/\(month)/\(year)&#34;
    } else if userDefaults.string(forKey: &#34;dateStyle&#34;) == &#34;YMD&#34; {
      cell.ideaDateLabel.text = &#34;\(year)/\(month)/\(day)&#34;
    }

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

    return cell
</code></pre>

<p>为 UserDefaults 设置 rgb 颜色:</p>

<pre><code>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 = &#34;Red: \(red!)&#34;
    redSlider.value = Float(red)
    greenLabel.text = &#34;Green: \(green!)&#34;
    greenSlider.value = Float(green)
    blueLabel.text = &#34;Blue: \(blue!)&#34;
    blueSlider.value = Float(blue)
}

@IBAction func sliderValueChanged(_ sender: Any) {
    red = redSlider.value
    green = greenSlider.value
    blue = blueSlider.value
    if red != nil {
      redLabel.text = &#34;Red: \(red!)&#34;
    }
    if green != nil {
      greenLabel.text = &#34;Green: \(green!)&#34;
    }
    if blue != nil {
      blueLabel.text = &#34;Blue: \(blue!)&#34;
    }
    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: &#34;red&#34;)
    userDefaults.removeObject(forKey: &#34;green&#34;)
    userDefaults.removeObject(forKey: &#34;blue&#34;)
    userDefaults.set(red, forKey: &#34;red&#34;)
    userDefaults.set(green, forKey: &#34;green&#34;)
    userDefaults.set(blue, forKey: &#34;blue&#34;)
    if userDefaults.float(forKey: &#34;isColorSet&#34;) == 0 {
      userDefaults.set(1, forKey: &#34;isColorSet&#34;)
    }
    _ = navigationController?.popToRootViewController(animated: true)
}
</code></pre>

<p> <a href="/image/szQfa.png" rel="noreferrer noopener nofollow">Screenshot with isColorSet = 0</a> </p>

<p> <a href="/image/VdL0T.png" rel="noreferrer noopener nofollow">Screenshot with isColorSet = 1</a> </p>

<p> <a href="/image/a4EwC.png" rel="noreferrer noopener nofollow">Screenshot of Color Picker</a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题是以下行中的一个简单错字:</p>

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

<p>绿色和蓝色值的分母应该是 255。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableView 背景颜色与 UITableViewCell 不同,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52709200/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52709200/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableView 背景颜色与 UITableViewCell 不同