Добавление вертикального стека в пользовательской ячейке и отображение / скрытие ответа на основе выбранной ячейки
class ViewController: UITableViewController {
var questions = [(question:String,answer:String)]()
var selectedQuestion = -1
override func viewDidLoad() {
super.viewDidLoad()
questions = [(question:"Question 1",answer:"Answer 1"),(question:"Question 2",answer:"Answer 2"),
(question:"Question 3",answer:"Answer 3"),(question:"Question 4",answer:"Answer 4"),
(question:"Question 5",answer:"Answer 5")]
self.view.backgroundColor = .white
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 50
tableView.register(FAQsCell.self, forCellReuseIdentifier: "FAQsCell")
}
}
extension ViewController {
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questions.count
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FAQsCell") as! FAQsCell
cell.questionLbl.text = questions[indexPath.row].question
cell.answerLbl.text = questions[indexPath.row].answer
if indexPath.row == selectedQuestion {
cell.backgroundColor = .groupTableViewBackground
cell.dropDownImgView.image = //upimage
cell.answerView.isHidden = false
} else {
cell.backgroundColor = .white
cell.dropDownImgView.image = //downimage
cell.answerView.isHidden = true
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedQuestion = indexPath.row
tableView.reloadData()
}
}
class FAQsCell: UITableViewCell {
let stackView = UIStackView()
let questionLbl = UILabel()
let dropDownImgView = UIImageView()
let answerView = UIView()
let answerLbl = UILabel()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
stackView.axis = .vertical
stackView.distribution = .fillProportionally
stackView.spacing = 5
stackView.alignment = .fill
stackView.translatesAutoresizingMaskIntoConstraints = false
addSubview(stackView)
let questionView = UIView()
questionView.translatesAutoresizingMaskIntoConstraints = false
questionView.heightAnchor.constraint(equalToConstant: 35).isActive = true
stackView.addArrangedSubview(questionView)
questionLbl.font = UIFont.boldSystemFont(ofSize: 18)
questionLbl.translatesAutoresizingMaskIntoConstraints = false
questionView.addSubview(questionLbl)
dropDownImgView.contentMode = .scaleAspectFit
dropDownImgView.translatesAutoresizingMaskIntoConstraints = false
questionView.addSubview(dropDownImgView)
answerView.translatesAutoresizingMaskIntoConstraints = false
answerView.heightAnchor.constraint(greaterThanOrEqualToConstant: 35).isActive = true
stackView.addArrangedSubview(answerView)
answerLbl.numberOfLines = 0
answerLbl.lineBreakMode = .byWordWrapping
answerLbl.font = UIFont.systemFont(ofSize: 17)
answerLbl.translatesAutoresizingMaskIntoConstraints = false
answerView.addSubview(answerLbl)
questionView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(10)-[questionLbl]-(10)-[dropDownImgView(25)]-(10@999)-|", options: [.alignAllCenterY], metrics: nil, views: ["questionLbl":questionLbl, "dropDownImgView": dropDownImgView]))
dropDownImgView.heightAnchor.constraint(equalTo: dropDownImgView.widthAnchor).isActive = true
questionView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(5)-[questionLbl(25)]-(5)-|", options: [], metrics: nil, views: ["questionLbl":questionLbl]))
answerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(10)-[answerLbl]-(10)-|", options: [], metrics: nil, views: ["answerLbl":answerLbl]))
answerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(5)-[answerLbl(>=25)]-(5)-|", options: [], metrics: nil, views: ["answerLbl":answerLbl]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(5)-[stackView]-(5@999)-|", options: [], metrics: nil, views: ["stackView":stackView]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(5)-[stackView]-(5)-|", options: [], metrics: nil, views: ["stackView":stackView]))
}
}