在 swift 中重新加载 PickerView 的特定行
Reloading Particular Row of PickerView in swift
我有一个由标签和图像组成的 pickerView。当 pickerview 在该特定图像上被点击时,我正在更新该图像。但要显示图像的变化,我必须重新加载 pickerView。当我重新加载它回到第一行。有什么方法可以只重新加载 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
}
没有这样的系统方法。但是,如果您使用的是自定义视图,您可以尝试这样的操作:
(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]
}
我有一个由标签和图像组成的 pickerView。当 pickerview 在该特定图像上被点击时,我正在更新该图像。但要显示图像的变化,我必须重新加载 pickerView。当我重新加载它回到第一行。有什么方法可以只重新加载 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
}
没有这样的系统方法。但是,如果您使用的是自定义视图,您可以尝试这样的操作:
(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]
}