Search code examples
iosswiftuiimageviewuipickerviewdidselectrowatindexpath

Update image on didSelectRow of pickerView in swift


I'm making a customer pickerView consisting of an image and a label. it is working perfectly fine. now i want to update the image of the selected picker row, like a tick if clicked once then it will be checked, if clicked again it will be unchecked(change of images). it is also working but NOT smoothly, they way it should. when i select the row, image didn't got changes at once, but when i scroll the picker it got changed. which i noticed is due to the fact that 'viewForRow' method is called later, it didn't get called on 'didSelectRow'. below is my code for viewForRow and DidSelectRow.

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

        let customeView = UIView(frame: CGRect(x: 0, y: 0, width: self.dropDown.rowSize(forComponent: 0).width, height: 30))

        // for label
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: customeView.frame.width, height: 30))
        label.textAlignment = .left
        label.text = FilteredQuestionNames[row]     

         // for tick image, array contains all the images 
        let image = FilteredQuestion_Images[row]
         imageView = UIImageView(image: image)

        imageView?.frame = CGRect(x: self.dropDown.rowSize(forComponent: 0).width - 36 , y: 0, width: 26, height: 26)

        customeView.addSubview(imageView!)
         customeView.addSubview(label)//pickerLabel)

        return customeView//pickerLabel

    }

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {        

                if selectedRows.contains(QuestionsText.text!){

                    self.imageName = "Unchecked Checkbox-26"

                }else{
                 selectedRows.append(QuestionsText.text!) // question added in selected question

                self.imageName = "Checked Checkbox-26"                
                 FilteredQuestion_Images[row] = UIImage(named: "Checked Checkbox-26")!

                  let image = FilteredQuestion_Images[row]
                  imageView = UIImageView(image: image)                    

    }

Solution

  • You are missing call pickerView.reloadComponent(component: yourComponentIndex)

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {        
    
                    if selectedRows.contains(QuestionsText.text!){
    
                        self.imageName = "Unchecked Checkbox-26"
    
                    }else{
                     selectedRows.append(QuestionsText.text!) // question added in selected question
    
                    self.imageName = "Checked Checkbox-26"                
                     FilteredQuestion_Images[row] = UIImage(named: "Checked Checkbox-26")!
    
                      let image = FilteredQuestion_Images[row]
                      imageView = UIImageView(image: image)                    
    
        }
        pickerView.reloadComponent(component: component)
    }
    

    Hope this helps