从一个选择器视图中选择的选项在另一个选择器视图 Swift 中重复 [英] Option selected from one of the picker view duplicates in another picker view Swift

查看:32
本文介绍了从一个选择器视图中选择的选项在另一个选择器视图 Swift 中重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 8 个问题的调查,每个问题都有一个选择器视图,其中包含从服务器加载的选项.这是在 UI 集合视图中.该调查最多可以回答 5 个问题,但是当我们添加 5 个以上的问题时,无论我们为问题 1 选择什么选项,问题 1 和 6 都会被选中.与问题 2 和 3 相同,它返回索引超出范围错误,我只看到 5 个答案而不是 8.任何帮助表示赞赏.

I've a survey with 8 questions and each question has a picker view with options loading from the server. This is in the UI Collection View. The survey works fine upto 5 questions but when we added more than 5, whatever the option we pick for the question 1 gets selected both for question1 and 6. same with question 2 and 3 which returns Index out of range error and I see only 5 answers instead of 8. Any help is appreciated.

CollectionView 的 Gif 文件

错误截图

这是我的代码:

        class QuestionCollectionViewController: UICollectionViewController {

        // Mark: - Properties

        let reuseIdentifier = "QuestionCell"

        let survey: Survey? = UserDefaults.getCodable(.survey)


        // number of cells is based on the
        override func collectionView(_ collectionView: UICollectionView,
                                     numberOfItemsInSection section: Int) -> Int {
            return survey?.questions.count ?? 0
        }



     override func collectionView(_ collectionView: UICollectionView,
                                 cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
                                                      for: indexPath) as! QuestionViewCell



  let data = survey?.questions[indexPath.item]
    cell.questionResponseTF.delegate = self
    cell.questionResponseTF.tag = indexPath.item
    cell.questionResponseTF.text = data.selectedValue
}

   func textFieldDidBeginEditing(_ textField: UITextField) {
   let index = IndexPath(item: textField.tag, section: 0)
   constructQuestionViewCell(cell,withQuestion:survey?.questions[index.item])
}
        // MARK: - Helpers

        /// populates the QuestionViewCell with SurveyQuestion and style ui elements
        private func constructQuestionViewCell(_ cell: QuestionViewCell, withQuestion question: SurveyQuestion? = nil) {
            cell.questionTitle.text = question?.title
            cell.questionTitle.numberOfLines = 0
            cell.questionTitle.sizeToFit()

    //        cell.questionDescription.text = question?.description
    //        cell.questionDescription.sizeToFit()

            cell.questionResponse.frame.origin.y = cell.questionTitle.frame.maxY + 7
            cell.questionResponse.inputAccessoryView = doneToolbar()
            cell.questionResponse.layer.borderColor = UIColor.lightGray.cgColor
            cell.questionResponse.layer.borderWidth = 1.0
            cell.questionResponse.layer.cornerRadius = 5.0

            // TODO refactor and remove else if keying off of "ratepain" once survey questions have been updated in the api
            if (question?.type == "number_list") {
                let options = question?.values ?? [" ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
                let picker = CustomPickerView(frame: .zero, textView: cell.questionResponse, options: options)
                cell.questionResponse.inputView = picker
                picker.reloadAllComponents()
            }
            else if (question?.key == "ratepain") {
                let options = [" ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
                let picker = CustomPickerView(frame: .zero, textView: cell.questionResponse, options: options)
                cell.questionResponse.inputView = picker
            }

            cell.sizeToFit()

        }

推荐答案

您需要在模型中添加一个额外的数据作为选定值,然后在选择选择器视图时将该值设置为文本字段

you need to add a extra data into model as selected value then set that value to textfield when selecting picker view

class QuestionCollectionViewController: UICollectionViewController,UITextFieldDelegate {

        // Mark: - Properties

        let reuseIdentifier = "QuestionCell"

        let survey: Survey? = UserDefaults.getCodable(.survey)

        // number of cells is based on the
        override func collectionView(_ collectionView: UICollectionView,
                                     numberOfItemsInSection section: Int) -> Int {
            return survey?.questions.count ?? 0
        }

        override func collectionView(_ collectionView: UICollectionView,
                                     cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
                                                          for: indexPath) as! QuestionViewCell
            let data = survey?.questions[indexPath.item]
            cell.questionResponse.delegate = self
            cell.questionResponse.tag = indexPath.item
            cell.questionResponse.text = data.selectedValue
            return cell
        }


        func textFieldDidBeginEditing(_ textField: UITextField) {
           let index = IndexPath(item: textField.tag, section: 0)
           constructQuestionViewCell(cell,withQuestion:survey?.questions[index.item])
        }



        private func constructQuestionViewCell(_ cell: QuestionViewCell, withQuestion question: SurveyQuestion? = nil) {
            cell.questionTitle.text = question?.title
            cell.questionTitle.numberOfLines = 0
            cell.questionTitle.sizeToFit()

            cell.questionResponse.frame.origin.y = cell.questionTitle.frame.maxY + 7
            cell.questionResponse.inputAccessoryView = doneToolbar()
            cell.questionResponse.layer.borderColor = UIColor.lightGray.cgColor
            cell.questionResponse.layer.borderWidth = 1.0
            cell.questionResponse.layer.cornerRadius = 5.0

            // TODO refactor and remove else if keying off of "ratepain" once survey questions have been updated in the api
            if (question?.type == "number_list") {
                let options = question?.values ?? [" ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
                let picker = CustomPickerView(frame: .zero, textView: cell.questionResponse, options: options)
                //cell.questionResponse.inputView = picker
                question?.selectedValue = picker.value
                picker.reloadAllComponents()
            }
            else if (question?.key == "ratepain") {
                let options = [" ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
                let picker = CustomPickerView(frame: .zero, textView: cell.questionResponse, options: options)
                //cell.questionResponse.inputView = picker
                question?.selectedValue = picker.value
            }

            cell.sizeToFit()

        }

这篇关于从一个选择器视图中选择的选项在另一个选择器视图 Swift 中重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆