从一个选择器视图中选择的选项在另一个选择器视图中重复,而Swift

问题描述 投票:0回答:2

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

Gif file of the CollectionView

这是我的代码:

    class QuestionCollectionViewController: UICollectionViewController {

    // Mark: - Properties

    let reuseIdentifier = "QuestionCell"

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

    var selectedCell:Int!

    // 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

        // manipulate a QuestionViewCell using SurveyQuestion data
        if selectedCell != nil{
          if selectedCell == indexPath.item{
            constructQuestionViewCell(cell, withQuestion: survey?.questions[indexPath.item])
          }
        }
      //  constructQuestionViewCell(cell, withQuestion: survey?.questions[indexPath.item])

        return cell
    }

  override  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
       self.selectedCell = indexPath.item
       collectionView.reloadData()
    }

    // 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()

    }
swift uicollectionviewcell uipickerview
2个回答
0
投票

我在这里看到多个问题。

  • 您正在传递给constructQuestionViewCell方法的单元格不是您要返回的。您需要传递带有地址的单元格,或者更好地从函数返回该单元格并将其传递给cellForItemAt
override func collectionView(_ collectionView: UICollectionView,
                                 cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
                                                      for: indexPath) as! QuestionViewCell
        return  constructQuestionViewCell(cell, withQuestion: survey?.questions[indexPath.item])

    }
private func constructQuestionViewCell(_ cell: QuestionViewCell, withQuestion question: SurveyQuestion? = nil) -> QuestionViewCell {
    cell.questionTitle.text = question?.title
    cell.questionTitle.numberOfLines = 0
    cell.questionTitle.sizeToFit()
    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()
    }
return cell
}
  • 此外,如果问题类型不等于“ number_list”,则您不会更新选项,因为单元格正在重用,它将保留旧值。

0
投票

在集合视图中获取选定的单元格。它可以解决此问题。

 var selectedCell:Int!

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,for: indexPath) as! QuestionViewCell
    if selectedCell != nil{
      if selectedCell == indexPath.item{
        constructQuestionViewCell(cell, withQuestion: survey?.questions[indexPath.item])
      }
    }
    return cell
 }

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    self.selectedCell = indexPath.item
    collectionView.reloadData()
 }
© www.soinside.com 2019 - 2024. All rights reserved.