在swift中重新加载PickerView的特定行

时间:2017-10-19 11:25:11

标签: ios swift uipickerview reloaddata uipickerviewdelegate

我有一个由标签和图像组成的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
}

1 个答案:

答案 0 :(得分: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]
}