Опираясь на ответ HamzaLH, вы можете сделать что-то вроде этого ...
импорт UIKit
класс TableViewController: UITableViewController {
var selectedRow: Int = 999 {
didSet {
tableView.beginUpdates()
tableView.endUpdates()
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == selectedRow { //assign the selected row when touched
let thisCell = tableView.cellForRow(at: indexPath)
if let thisHeight = thisCell?.bounds.height {
return thisHeight + 50
}
}
return 60 //return a default value in case the cell height is not available
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedRow = indexPath.row
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
cell.detailTextLabel?.text = "test"
return cell
}
}
Я оживляю расширение высоты с помощью didSet, когда выбранный ряд изменяется.
Кроме того, не забывайте, что вам все равно может понадобиться подключить ваш источник данных и делегировать, перетаскивая выходы в Интерфейсном Разработчике на ваш View Controller в раскадровке. После этого вам все еще нужно добавить это в ViewDidLoad в swift-файле вашего ViewController.
tableView.delegate = self
tableView.dataSource = self
У меня также есть Outlet, объявленный для tableView, как показано ниже, и подключенный в раскадровке Interface Builder.
@IBOutlet weak var tableView: UITableView!
![enter image description here](https://i.stack.imgur.com/mOKLB.png)