添加和删​​除单元格选择视图

时间:2017-02-23 16:47:28

标签: ios swift uitableview swift3

我有一个tableview,在每个单元格中显示一些数据,当选择单元格时,它应该展开并显示更多数据。我有代码的基础知识,但是当选择单元格时,直到选择了另一个单元格才会显示数据。选择时,数据似乎会复制到其他单元格中。我确信我只需要移动方法调用,尽管我认为我已经尝试了所有组合,但我找不到解决方案。下面是我认为重要的代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let answers = questions[indexPath.row].answers?.allObjects as! [Answer]
    let cell = tableView.cellForRow(at: indexPath as IndexPath) as! CollectedDataTableViewCell

    switch selectedIndexPath {
    case nil:
        cell.contentView.viewWithTag(indexPath.row + 1)?.removeFromSuperview()
        selectedIndexPath = indexPath as NSIndexPath?
    default:
        if selectedIndexPath! as IndexPath == indexPath {
            addAnswerViews(cell: cell, answers: answers, index: indexPath.row + 1)
            selectedIndexPath = nil
        } else {
            cell.contentView.viewWithTag(indexPath.row + 1)?.removeFromSuperview()
            selectedIndexPath = indexPath as NSIndexPath?
        }
    }

    tableView.reloadRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.automatic)

    self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}

func addAnswerViews(cell: CollectedDataTableViewCell, answers: [Answer], index: Int) {

    var extra = CGFloat(0)
    var percentage: Double = 0

    let percentFormatter = NumberFormatter()

    percentFormatter.numberStyle = .percent
    percentFormatter.multiplier = 100
    percentFormatter.minimumFractionDigits = 1
    percentFormatter.maximumFractionDigits = 2

    answers.forEach { answer in
        let height = self.height - 6
        let width = cell.contentView.frame.width
        let view = UIView(frame: CGRect(x: 0, y: height + extra, width: width, height: height))
        view.tag = index

        let answerLabel = UILabel(frame: CGRect(x: 15, y: 0, width: width - 30, height: height/2))
        answerLabel.backgroundColor = .clear
        answerLabel.adjustsFontSizeToFitWidth = true
        answerLabel.text = answer.text

        if answer.timesSelected == 0 || answer.timesSelectedCorrectly == 0 {

            percentage = 0
        } else {

            percentage = Double(answer.timesSelectedCorrectly)/Double(answer.timesSelected)
        }

        var percentString = percentFormatter.string(from: NSNumber(value: percentage))
        if percentage == 1.0 { percentString = "100%"}
        if percentage == 0.0 { percentString = "0%" }

        let timesSelectedCorrectlyLabel = UILabel(frame: CGRect(x: 15, y: height/2, width: width/2 - 15, height: height/2))
        timesSelectedCorrectlyLabel.backgroundColor = .clear
        timesSelectedCorrectlyLabel.adjustsFontSizeToFitWidth = true
        timesSelectedCorrectlyLabel.text = "Times selected correctly: \(answer.timesSelectedCorrectly) - \(percentString!)"

        let timesSelectedLabel = UILabel(frame: CGRect(x: width/2 - 15, y: height/2, width: width/2 - 15, height: height/2))
        timesSelectedLabel.backgroundColor = .clear
        timesSelectedLabel.adjustsFontSizeToFitWidth = true
        timesSelectedLabel.text = "Times selected: \(answer.timesSelected)"

        view.addSubview(answerLabel)
        view.addSubview(timesSelectedCorrectlyLabel)
        view.addSubview(timesSelectedLabel)

        cell.contentView.addSubview(view)
        extra += height
    }
}

1 个答案:

答案 0 :(得分:2)

修改你的didselectRowAtIndexPath,如下所示:)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let answers = questions[indexPath.row].answers?.allObjects as! [Answer]
    let cell = tableView.cellForRow(at: indexPath as IndexPath) as! CollectedDataTableViewCell

    if self.previouslySelectedIndexPath != nil {
        cell.contentView.viewWithTag(previouslySelectedIndexPath.row + 1)?.removeFromSuperview()
    }
    addAnswerViews(cell: cell, answers: answers, index: indexPath.row + 1)

    if self.previouslySelectedIndexPath != nil {
       tableView.reloadRows(at: [indexPath,self.previouslySelectedIndexPath], with: UITableViewRowAnimation.automatic)
    }
    else {
       tableView.reloadRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.automatic)
    }
    self.previouslySelectedIndexPath = indexPath
    self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}