在 swift 中更新 pickerView 的 didSelectRow 上的图像

Update image on didSelectRow of pickerView in swift

我正在制作一个由图像和标签组成的客户选择器视图。它工作得很好。现在我想更新 selected 选择器行的图像,就像一个勾号,如果单击一次,它将被选中,如果再次单击,它将被取消选中(图像更改)。它也在工作,但不顺利,他们应该这样做。当我 select 行时,图像没有立即发生变化,但是当我滚动选择器时它发生了变化。我注意到这是因为 'viewForRow' 方法稍后被调用,它没有在 'didSelectRow' 上被调用。下面是我的 viewForRow 和 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)                    

    }

您未接来电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)
}

希望对您有所帮助