UITableView

时间:2018-02-13 14:33:40

标签: ios swift uitableview

class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource{
    @IBOutlet weak var tableView: UITableView!

    var questions:[Question] = []
    var sectionCountGlobal = 0
    override func viewDidLoad() {
        super.viewDidLoad()

        questions = fillQuestions()
    }

    func fillQuestions()-> [Question]{
        var temp : [Question] = []
        var choices : [Choice] = []
        let choice = Choice(id: 1, text: "choice ", status: 1, questionId: 1)
        choices.append(choice)
        choices.append(choice)
        choices.append(choice)
        choices.append(choice)
        choices.append(choice)
        choices.append(choice)

        let q1 = Question(id: 1, text: "Ahmad 55 years old man with a history of hypertension and hypocholesteremia was in a wedding and during the party he starts to feel chest pain and dizzy, his wife brought him to the emergency department. The ER nurse checked his vital signs: BP 88/50, HR: 45, RR:10, SPaO2: 90% and O2 per nasal cannula was started at 4l/minute. Few seconds later Mr.Ahmad lost consciousness and the code blue team were activated.", choices: choices)
        let q2 = Question(id: 1, text: "question 2", choices: choices)
        let q3 = Question(id: 1, text: "question 3", choices: choices)

        temp.append(q1)
        temp.append(q2)
        temp.append(q3)
        return temp
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return questions.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        sectionCountGlobal = section
        return questions[section].choices.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0{
            let questionTextCell = tableView.dequeueReusableCell(withIdentifier: "QuestionTextCell") as! QuestionTextCell
            questionTextCell.setQuestionText(text: questions[indexPath.section].text)
            return questionTextCell
        }else{
            let choiceCell = tableView.dequeueReusableCell(withIdentifier: "ChoiceCell") as! ChoiceCell
            choiceCell.choiceText.text = questions[indexPath.section].choices[indexPath.row].text
            return choiceCell
        }
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let questionNumber = "Q" + String(section+1)
        return questionNumber
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 3
    }

    override func viewWillAppear(_ animated: Bool) {
       tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
    }
}

我正在开发一个测验应用程序,每个问题有多个选项,所以当检查单元格中的单选按钮并滚动到其他单元格时,我发现其他单元格已经过检查而没有触及它们是什么解决方案。

我尝试了不同的细胞重复使用方法,还有prepareForReuse(),没有任何方法可以独立处理每个细胞而不受其他细胞的影响,我不知道它来自服务器的问题数量。

question1 i checked the first choice

question3 the choice checked by itself

2 个答案:

答案 0 :(得分:1)

cellForRowAt实施中,您必须根据是否选中来重置单元格的状态。由于单元格重用,您可以获得之前选择的单元格,但现在不应该选择 - 在这种情况下,您必须告知单元格未被选中(反之亦然):

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

    if indexPath.row == 0{
        let questionTextCell = tableView.dequeueReusableCell(withIdentifier: "QuestionTextCell") as! QuestionTextCell
        questionTextCell.setQuestionText(text: questions[indexPath.section].text)
        return questionTextCell
    } else {
        let choiceCell = tableView.dequeueReusableCell(withIdentifier: "ChoiceCell") as! ChoiceCell

        // here detect if the cell should be selected and set it accordingly, so something like:
        let isSelected = isSelectedChoice(questions[indexPath.section].choices[indexPath.row])
        choiceCell.isSelected = isSelected
        // of course this is just a mockup, since I don't know exactly how you manage selection,
        // but it should get you on the right path

        choiceCell.choiceText.text = questions[indexPath.section].choices[indexPath.row].text
        return choiceCell
    }
}

答案 1 :(得分:0)

代码中的问题是您没有更改单选按钮的状态。从didSelectRowAt方法中选择选项时,您必须更改所选的状态。根据您的选择模型,您可以更改特定选择状态的状态。以下两种方法都可以管理您的选择(您的状态变量应该是Bool类型):

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0{
        let questionTextCell = tableView.dequeueReusableCell(withIdentifier: "QuestionTextCell") as! QuestionTextCell
        questionTextCell.setQuestionText(text: questions[indexPath.section].text)
        return questionTextCell
    }else{
        let choiceCell = tableView.dequeueReusableCell(withIdentifier: "ChoiceCell") as! ChoiceCell

        // update your radio button UI
        choiceCell.radioButton.isSelected = questions[indexPath.section].choices[indexPath.row].status

        choiceCell.choiceText.text = questions[indexPath.section].choices[indexPath.row].text
        return choiceCell
    }
}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    questions[indexPath.section].choices[indexPath.row].status = !questions[indexPath.section].choices[indexPath.row].status
    tableView.reloadData()

}