更新swift中pickerView的didSelectRow上的图像

时间:2017-08-08 11:32:23

标签: ios swift uiimageview uipickerview didselectrowatindexpath

我正在制作一个由图像和标签组成的客户选择器视图。它工作得非常好。现在我想更新所选选择器行的图像,如果点击一次就会勾选,然后检查它,如果再次点击它将被取消选中(图像更改)。它也工作但不顺利,他们应该这样做。当我选择行时,图像没有立即改变,但是当我滚动拾取器时它变了。我注意到这是因为'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)                    

    }

1 个答案:

答案 0 :(得分:0)

您缺少来电@MyList.CompanyNo

pickerView.reloadComponent(component: yourComponentIndex)

希望这有帮助

相关问题