【问题标题】:Select cell from a saved indexPath从保存的 indexPath 中选择单元格
【发布时间】:2020-09-06 23:54:37
【问题描述】:

我正在尝试创建一个使用 indexPath 选择单元格的 tableview。

我已保存所选单元格的 indexPath 并将其保存到我的每个问题的数据数组中。但是,当我重新加载 tableView 时,如何让 indexPath 将该特定单元格的背景视图更改为我选择上一个按钮时的外观。

var dataArray: [MultipleChoice] = [
    MultipleChoice(
        question: "Question 1",
        options: ["Answer 1", "Answer 2", "Answer 3", "Answer 4"],
        rightAnswer: "Answer 1",
        subject: "General",
        idxPath: [0,0]),
    MultipleChoice(
        question: "Question 2",
        options: ["Answer 1", "Answer 2", "Answer 3", "Answer 4"],
        rightAnswer: "Answer 2",
        subject: "General",
        idxPath: [0,0]),


@IBAction func nextButtonPressed(_ sender: Any) {

    if var index = dataBrain.questionNumber {
        if index + 1 < dataBrain.dataArray.count {

            index += 1
            dataBrain.questionNumber = index
            scoreLabel.text = "\(dataBrain.questionNumber! + 1)/\(dataBrain.dataArray.count)"
            answerHidden = true
            table.reloadData()

        } 

}

@IBAction func previousButtonPressed(_ sender: Any) {

    if var index = dataBrain.questionNumber {
        if index - 1 >= 0 {

            index -= 1
            dataBrain.questionNumber = index
            scoreLabel.text = "\(dataBrain.questionNumber! + 1)/\(dataBrain.dataArray.count)"
            table.reloadData()

        }

}

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

let cell = tableView.dequeueReusableCell(withIdentifier: AnswerTableViewCell.identifier) as! AnswerTableViewCell

        //Load Options:
        if let index = dataBrain.questionNumber {
            let data = dataBrain.dataArray[index]
            cell.answerLabel.text = data.options![indexPath.row - 1]
            cell.selectionStyle = .none
        }

        //Load Right/Wrong Options
        let index = dataBrain.questionNumber
        let data = dataBrain.dataArray[index!]
        let rightAnswer = data.rightAnswer!

        if cell.answerLabel.text == rightAnswer {
            cell.confirmButton.setImage(UIImage(systemName: "checkmark.circle"), for: .normal)
            cell.confirmButton.imageView?.tintColor = UIColor.green
        } else {
            cell.confirmButton.setImage(UIImage(systemName: "xmark.circle"), for: .normal)
            cell.confirmButton.imageView?.tintColor = UIColor.red
        }

        //Load Background Color/Text
        cell.delegate = self
        cell.selectedBackgroundView = .none
        cell.confirmButton.isHidden = answerHidden

        let selectedCell = dataBrain.dataArray[index!].idxPath
        if selectedCell == [0,0] {
            cell.answerView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
            cell.answerLabel.textColor = #colorLiteral(red: 0.4228360057, green: 0.4478931427, blue: 0.4731111526, alpha: 1)
        } else {
            table.selectRow(at: selectedCell, animated: false, scrollPosition: .none)
        }

        return cell

【问题讨论】:

    标签: ios swift uitableview multiple-choice indexpath


    【解决方案1】:

    当您实现tableView(_ tableView: UITableView, cellForRowAt ... 时,您的工作是为给定的 indexPath 返回一个单元格——不要更改表格。您的事实来源是 dataBrain.dataArray - 获取信息,将其放入单元格中,然后返回。就是这样。

    所以,在这段代码中:

    let selectedCell = dataBrain.dataArray[index!].idxPath
    if selectedCell == [0,0] {
        cell.answerView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        cell.answerLabel.textColor = #colorLiteral(red: 0.4228360057, green: 0.478931427, blue: 0.4731111526, alpha: 1)
    } else {
        table.selectRow(at: selectedCell, animated: false, scrollPosition: .none)
    }
    

    if 应该更像这样:

    if selectedCell[0] == indexPath.section, selectedCell[1] == indexPath.row {
        // The cell I am making is selected -- set it up
    } else {
        // The cell I am making is not selected, set it up also
        // (it could be a reused cell that is colored for selection, so ALWAYS set the properties.
    }
    

    请勿拨打table.selectRow

    现在,当您想要更改单元格时,只需重新加载即可。 reloadData(不理想,但您可以这样做以确保一切正常)或其他针对特定更改的重新加载方法。

    【讨论】:

      猜你喜欢
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 2015-11-21
      • 1970-01-01
      • 2018-01-04
      • 2019-02-20
      • 2015-05-31
      相关资源
      最近更新 更多