Swift 3 - 在多个自定义单元格中重用单元格的问题

Hee*_*low 2 uitableview custom-cell ios swift

在 UITableview 中向下滚动时遇到问题。当单元格被重用时,表格向我显示了具有旧内容的单元格。

问题如下:

Swift 想要重用旧单元格,但没有正确清除旧单元格中的旧内容。这会导致单元格具有旧内容,尽管我正在为单元格提供新数据。

UITableView 的架构,如果如下:

  • 每个自定义单元格都有自己的标识符
  • 每个自定义单元格都在一个自己的类中分开

问题截图:

问卷开始截图

截屏

向下滚动的表格

截屏

这里的问题是“Handedness”-Cell,它显示了单元格编号 3(因为单元格的重用),这是不对的

numberOfSection-方法

override func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}
Run Code Online (Sandbox Code Playgroud)

numberOfRowsInSection-方法

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(section == 0){
        return questionnaireStructure.count
    } else {
        return 1
    }
}
Run Code Online (Sandbox Code Playgroud)

cellForRowAt 方法

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // first section is the normal Questionnaire
    if(indexPath.section == 0){

        // current questionnaireStructure
        let questStruct:QuestionnaireStructure? = questionnaireStructure[indexPath.row]

        // current cell is a "Headline"
        if(questStruct?.elementtype == "elements/headlines"){
            let cell = tableView.dequeueReusableCell(withIdentifier: "HeadlineStructureCellID", for: indexPath) as! Headline
            cell.headline.text = questStruct?.headline
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            return cell
        } else if(questStruct?.elementtype == "elements/texts"){
            // current cell is a "texts"
            let cell = tableView.dequeueReusableCell(withIdentifier: "TextsStructureCellID", for: indexPath) as! Texts
            cell.textsLabel.text = questStruct?.text
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            return cell
        } else if(questStruct?.questiontype == "Slider"){
            // currrent cell is a "slider-Question"
            let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionSliderStructureCellID", for: indexPath) as! Slider
            cell.sliderQuestion.text = questStruct?.question
            cell.selectionStyle = UITableViewCellSelectionStyle.none

            let values = (questStruct?.values)!
            let valueArray = values.array as! [Values]
            cell.slider.minimumValue = Float(valueArray[0].min)
            cell.slider.maximumValue = Float(valueArray[0].max)
            let answers = (questStruct?.answers)!
            let answerArray = answers.array as! [Answers]
            cell.minLabel.text = answerArray[0].label
            cell.maxLabel.text = answerArray[1].label

            return cell
        } else if(questStruct?.questiontype == "SingleChoice"){
            let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionSingleChoiceStructureCellID", for: indexPath) as! SingleChoiceCell
            let radioButtonController = SSRadioButtonsController()
            radioButtonController.delegate = self
            radioButtonController.shouldLetDeSelect = true
            cell.radioButtonController = radioButtonController
            cell.updateCellData(questStruct: questStruct!, indexInTable: indexPath.row)

            return cell
        } else if(questStruct?.questiontype == "MultipleChoice"){
            let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionMultipleChoiceStructureCellID", for: indexPath) as! MultipleChoiceCell
            cell.multQuestionLabel.text = questStruct?.question
            cell.questStruct = questStruct

            return cell
        } else if(questStruct?.questiontype == "YesNoSwitch"){
            let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionYesNoSwitchStructureCellID", for: indexPath) as! YesNoSwitch
            cell.yesNoQuestion.text = questStruct?.question
            cell.selectionStyle = UITableViewCellSelectionStyle.none

            return cell
        } else if(questStruct?.questiontype == "TextDate"){
            let cell = tableView.dequeueReusableCell(withIdentifier: "Datepicker", for: indexPath) as! DatePicker
            cell.question.text = questStruct?.question
            cell.selectionStyle = UITableViewCellSelectionStyle.none

            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionSingleChoiceStructureCellID", for: indexPath) as! SingleChoiceCell
            //cell.singleChoiceLabel.text = questStruct?.question
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            return cell
        }

    } else {
        //last section is the save button
        // show the save button when the Questionnaire is loaded
        if(questionnaireStructure.count != 0){
            let cell = tableView.dequeueReusableCell(withIdentifier: "SaveStructureCellID", for: indexPath) as! SaveQuestionnaire
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TextsStructureCellID", for: indexPath) as! Texts
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            return cell
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我检查的内容:

  • “questStruct”的数据提供最新数据
  • 覆盖“prepareForReuse”-Methode 没有成功

Don*_*Mag 5

这里:

    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionSingleChoiceStructureCellID", for: indexPath) as! SingleChoiceCell
        //cell.singleChoiceLabel.text = questStruct?.question
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        return cell
    }
Run Code Online (Sandbox Code Playgroud)

您需要“重置”单元格以防它被重用。选项是:

  1. reset()在单元格中编写一个函数,以清除任何分配的数据并显示“默认”内容,或

  2. 创建一个空的questStruct并调用cell.updateCellData(questStruct: questStruct!, indexInTable: indexPath.row)

选项 1. 可能是最简单和最直接的。

  • `prepareForReuse()` 是放置 `reset()` 内容的地方。 (2认同)
  • 太感谢了!!!:-) 这是代码中的错误。我很感激你帮助了我 (2认同)