Home > Blockchain >  Using UIColorPickerViewController to change the color of cell background color always returns black
Using UIColorPickerViewController to change the color of cell background color always returns black

Time:11-01

I'm developing an app for my school that helps students better understand their grades by getting an analysis on their portfolio of assignments for each class. Right now I am at the point of letting the user create the classes they are in and customize the information within it that is displayed in a tableview with custom cells. The user gives the majority of the information in a child view where they input information such as class name, teacher, grade weighting, etc. I wanted to give the user the ability to change the color of the cell once they are viewing the TableView with all the cells - classes - they made. I decided to go about this by having the cells have a UIButton that they can click on for their determined cell to then pull up a UIColorPickerViewController.

What I wanted to happen was...

  1. User taps button in cell
  2. UIPickerViewController is pulled up
  3. User selects their desired color
  4. User exits UIPickerViewController
  5. Cell is changed to the color

What is actually happening is this

  1. User taps button in cell
  2. Cell background becomes black right as UIPickerViewController is presented
  3. User selects their desired color
  4. User exits UIPickerViewController
  5. Cell remains black

I used a delegate to send the information from the cells and then I used the "colorPickerViewControllerDidFinish()" function and it's still not working out. When I did some debugging I found that the value of the UIColorPickerViewController is actually being stored in the variable I am using, but only after I have already assigned it's value to the cell background so I'm unsure what to do. As you can probably tell, I'm new to swift so apologies for any stupid mistakes in my code.

Custom Cell File

// 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 Extensions

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
    }
}

CodePudding user response:

You should implement one more UIColorPickerViewControllerDelegate method:

func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
    cellBackgroundColor = viewController.selectedColor
}

CodePudding user response:

It's a great start! As a really direct answer to your original question:

The reason for your problem is that in your didTapButton function you are presenting the color picker, but then immediately telling the app that the user is done with the picker, and then immediately setting the background color to cellBackgroundColor, which I assume has a default value of UIColor.black.

Instead you should delete those last 2 lines in your didTapButton function - just initialize the picker, set the delegate, and present the picker. Then the delegate method you chose - colorPickerViewControllerDidFinish isn't really the correct one for your purpose. Instead consider using the didSelect delegate method (see docs). When that method is called it will pass you the color the user selected, which you can simply use to set your background color and refresh your tableView cell if needed.

Since you mention you are a new Swift dev I will also mention that UITableView reuses its cells, so simply setting the background color of a cell once will not have the result you are expecting. You will see that as you scroll the cells up and down the colors will change in the various cells, so ultimately you'll need to store the color selections in another way so that each time a cell is being dequeued you can set the correct color based on user input. That part is outside of the scope of the original question, but just letting you know.

  • Related