首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用UIColorPickerViewController更改单元格背景色的颜色总是返回黑色

使用UIColorPickerViewController更改单元格背景色的颜色总是返回黑色
EN

Stack Overflow用户
提问于 2022-10-31 14:00:57
回答 2查看 45关注 0票数 1

我正在为我的学校开发一个应用程序,通过对每个班级的作业组合进行分析,帮助学生更好地理解他们的成绩。现在,我正在允许用户创建他们所在的类,并自定义其中的信息,这些信息显示在带有自定义单元格的表视图中。用户在子视图中提供大部分信息,输入诸如类名、教师、等级加权等信息。我想让用户在查看所有单元格--类--时,能够更改单元格的颜色。我决定这样做,让细胞有一个UIButton,他们可以点击这个决定的单元格,然后拉一个UIColorPickerViewController。

我想要的是..。

  1. 用户点击单元格中的按钮
  2. UIPickerViewController被拉上来了
  3. 用户选择他们想要的颜色
  4. 用户退出UIPickerViewController
  5. 单元格更改为颜色。

实际上正在发生的是

  1. 用户点击单元格中的按钮
  2. 单元背景随着UIPickerViewController的出现而变黑,
  3. 用户选择他们想要的颜色
  4. 用户退出UIPickerViewController
  5. 细胞保持黑色

我使用委托从单元格发送信息,然后使用"colorPickerViewControllerDidFinish()“函数,但它仍然不起作用。当我进行一些调试时,我发现UIColorPickerViewController的值实际上存储在我所使用的变量中,但只是在我已经将它的值分配给单元格背景之后,所以我不知道该做什么。正如您可能知道的那样,对于我代码中的任何愚蠢错误,我都是新手。

自定义单元文件

代码语言:javascript
复制
// Protocol for VC's to conform to so they can distinguish which cell has a button being tapped

protocol newlyCreatedCellDelegate: AnyObject
{
    func didTapButton(title: String, cellView: UIView)
}


class newlyCreatedClass: UITableViewCell {
    
    // Telling the delegate what to do once they are assigned
    weak var delegate: newlyCreatedCellDelegate?
    
    
    
    
    @IBOutlet weak var classContentView: UIView!
    
    @IBOutlet weak var classUIView: UIView!
    
    @IBOutlet weak var classNameLabel: UILabel!
    @IBOutlet weak var classTeacherNameLabel: UILabel!
    
    @IBOutlet weak var pointType1NameLabel: UILabel!
    @IBOutlet weak var pointType2NameLabel: UILabel!
    @IBOutlet weak var pointType3NameLabel: UILabel!
    
    
    @IBOutlet weak var percent1Label: UILabel!
    @IBOutlet weak var percent2Label: UILabel!
    @IBOutlet weak var percent3Label: UILabel!
    
    
    @IBOutlet weak var colorButton: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        colorButton.layer.cornerRadius = 21
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
   // Essentially creating the prep list for the delegate. If they are called - whoever it is - they will go through this 'checklist'
    @IBAction func colorButtonTapped(_ sender: Any)
    {

        delegate?.didTapButton(title: classNameLabel.text!, cellView: classUIView)
        
        
    }
    
}

ViewController扩展

代码语言:javascript
复制
extension ClassSelection: newlyCreatedCellDelegate
{
    func didTapButton(title: String, cellView: UIView)
    {
        let colorPickerVC = UIColorPickerViewController()
        colorPickerVC.delegate = self
        
        present(colorPickerVC, animated: true, completion: nil)
        
        colorPickerViewControllerDidFinish(colorPickerVC)
        
        // 'cellBackgroundColor' is a variable declared in the VC to transfer the UIColor value
        cellView.backgroundColor = cellBackgroundColor        
    }
    
    
}

extension ClassSelection: UIColorPickerViewControllerDelegate
{
    func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
        cellBackgroundColor = viewController.selectedColor
    }
}
EN

回答 2

Stack Overflow用户

发布于 2022-10-31 15:51:15

您应该再实现一个UIColorPickerViewControllerDelegate方法:

代码语言:javascript
复制
func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
    cellBackgroundColor = viewController.selectedColor
}
票数 0
EN

Stack Overflow用户

发布于 2022-10-31 18:48:30

这是个很好的开始!作为对你最初问题的一个真正直接的回答:

造成问题的原因是,在您的didTapButton函数中,您呈现的是颜色选择器,然后立即告诉应用程序用户已经完成了选择,然后立即将背景色设置为cellBackgroundColor,我假设它的默认值为UIColor.black

相反,您应该删除didTapButton函数中的最后2行--只需初始化选择器、设置委托和呈现选择器。然后您选择的委托方法- colorPickerViewControllerDidFinish并不是真正适合您的方法。相反,可以考虑使用didSelect委托方法(参见文档)。当调用该方法时,它将传递用户选择的颜色,您可以使用该颜色来设置背景色,并在需要时刷新tableView单元格。

由于您提到您是一个新的Swift,我还将提到UITableView重用它的单元格,因此只设置单元格的背景色一次就不会得到您预期的结果。您将看到,当您上下滚动单元格时,颜色将在不同的单元格中发生变化,因此最终需要以另一种方式存储颜色选择,这样每次单元格退出队列时,您都可以根据用户输入设置正确的颜色。这部分超出了最初问题的范围,只是让你知道而已。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74264449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档