Select 来自保存的 indexPath 的单元格

Select cell from a saved indexPath

我正在尝试创建一个使用 indexPath select单元格的表格视图。

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

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

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

因此,在这段代码中:

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(不理想,但您可以这样做以确保一切正常),或其他针对特定更改的重新加载方法。