ios - 快速更新 pickerView 的 didSelectRow 上的图像

标签 ios swift uiimageview uipickerview didselectrowatindexpath

我正在制作一个由图像和标签组成的客户 pickerView。它工作得很好。现在我想更新所选选择器行的图像,就像一个勾号,如果单击一次,它将被选中,如果再次单击,它将被取消选中(更改图像)。它也正在工作,但并不顺利,他们应该这样做。当我选择该行时,图像没有立即发生变化,但是当我滚动选择器时,它发生了变化。我注意到这是由于“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)                    

    }

最佳答案

您缺少调用 uipickerView.reload Component(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)
}

希望这有帮助

关于ios - 快速更新 pickerView 的 didSelectRow 上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45567196/

相关文章:

iphone - Grand Central Dispatch (GCD) 与 performSelector - 需要更好的解释

ios - Core-Plot 选择一个点

ios - ORKTextAnswerFormat 正则表达式验证

ios - 在 ios 中使用旋转在圆形 View 中设置图像

ios - 如何在TableController的ImageView中正确添加图片?

ios - 需要解释一下 NSLayoutAttributeBaseline

ios - Swift 包管理器与未版本化包有关的问题(例如 : firebase-ios-sdk)

ios - 所以。我试图让我的文本字段下的字符数在屏幕上增加,但它没有增加

swift - 在 Swift 中获取整数的字符 ASCII 值

ios - 如何在 iOS 中创建 imageView 点击事件?