我有一個表格顯示每個單元格中的一些數據,當選中單元格時,它應該展開並顯示更多數據。我有代碼的基本知識,但是當單元格被選中時,數據不會顯示,直到選中另一個單元格。數據似乎在選中時自動複製到其他單元格中。我相信我只需要移動方法的調用,儘管認爲我已經嘗試了所有組合,但找不到解決方案。繼承人的代碼,我想事項:添加和刪除單元格選擇視圖
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
}
}
你必須重新加載的UITableView節下面,那些你想擴大。 – iParesh
即時做這didSelectRow – Wazza
第二行還需要設置=> func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int) - > Int { return elements.count } – iParesh