I have a pickerView consisting of a label and image. i'm updating that image when pickerview is tapped on that particular. but to show the change of image i have to reload pickerView. and when i reload it goes back to the first row. is there any way to reload only that particular row of pickerView?
Code:
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()
}
}
Code of 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
}
No such system method. However, if you are using custom view, you can try something like this:
(pickerView.view(forRow: <#T##Int#>, forComponent: <#T##Int#>) as? MyCustomView).image = newImage
Edit:
First you need you need to add view tags to your custom view elements(you can define two constants for this):
label.tag = 10
imageView.tag = 20
And after a selection occurs, you need to refresh your view's state. You can do that by:
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]
}