2017-10-19 89 views
0

我有一個pickerView由一個標籤和圖像組成。當pickerview被點擊時,我正在更新該圖像。但要顯示圖像的變化,我必須重新加載pickerView。當我重新加載它回到第一行。有沒有辦法重新加載只有特定行的pickerView?在swift中重新加載pickerView的特定行

代碼:

if selectedRows.contains(contactPerson.text!) { 
         FilteredQuestion_Images[row] = UIImage(named: "Unchecked Checkbox-26")! 
        let image = FilteredQuestion_Images[row] 
        imageView = UIImageView(image: image) 

        if let itemToRemoveIndex = selectedRows.index(of: contactPerson.text!) { 
         selectedRows.remove(at: itemToRemoveIndex) 
         ContactPersons_Multiple.remove(at: itemToRemoveIndex) 
        } 

        self.dropdownPicker.reloadAllComponents() 

       }else { 


        selectedRows.append(contactPerson.text!) 
        self.imageName = "Checked Checkbox-26" 

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

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

        self.dropdownPicker.reloadAllComponents() 

        } 
       } 

代碼ViewForRow的:

 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.dropdownPicker.rowSize(forComponent: 0).width, height: 30)) 

      // for label 
      let label = UILabel(frame: CGRect(x: 0, y: 0, width: customeView.frame.width - 35, height: 30)) 
      label.textAlignment = .left 

      label.text = self.ContactPers[row] 

      if FilteredContactsSelected == true { 
       label.text = FilteredContactsNames[row] 
      } 


      let image = FilteredQuestion_Images[row]//UIImage(named: imageName) 
      imageView = UIImageView(image: image) 



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

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

      return customeView//pickerLabel 
} 

回答

0

沒有這樣的系統的方法。但是,如果您使用的是自定義視圖,你可以嘗試這樣的事:

(pickerView.view(forRow: <#T##Int#>, forComponent: <#T##Int#>) as? MyCustomView).image = newImage 

編輯:

首先,你需要,你需要查看標籤添加到您的自定義視圖元素(可以定義兩個常量這個):

label.tag = 10 
imageView.tag = 20 

而選擇發生後,你需要刷新你的視圖的狀態。你可以這樣做:

if let myView = pickerView.view(forRow: row, forComponent: 0) as? UIView, 
     let label = myView.viewWithTag(10) as? UILabel, 
     let imageView = myView.viewWithTag(20) as? UIImageView{ 

     label.text = FilteredContactsNames[row] 
     imageView.image = ilteredQuestion_Images[row] 
} 
+0

在哪裏準確地調用它? –

+0

你實現了'可選公共func pickerView(_ pickerView:UIPickerView,viewForRow行:Int,forComponent組件:Int,重用view:UIView?) - > UIVick'方法'UIPickerViewDelegate'? –

+0

是的,我正在使用它。 –

相關問題