У меня есть табличное представление внутри UIViewController. Это табличное представление имеет динамическую таблицу с кнопками.
Я установил функцию для каждой кнопки внутри TableViewCell следующим образом:
class ButtonTableViewCell: UITableViewCell {
let defaults = UserDefaults.standard
var targetTitle: String!
@IBOutlet weak var buttonDisplay: UIButton!
func setButton(title: String) {
buttonDisplay.setTitle(title.uppercased(), for: .normal)
targetTitle = title
buttonDisplay.addTarget(self, action: #selector(changeValue(_:)), for: .touchUpInside)
}
@objc func changeValue(_ sender: Any) {
currentCar = targetTitle
defaults.set(currentCar, forKey: "currentCar")
}
}
Как я могу закрыть UIViewController, который содержит TableView внутри функции @objc func changeValue(_ sender: Any)
?
Кроме того, поскольку кнопки добавляются динамически, мне также нужна динамическая высота для самого табличного представления - как я могу настроить табличное представление для каждой добавленной ячейки?
UIViewController:
class DropdownViewController: UIViewController {
@IBOutlet weak var tableViewButtons: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableViewButtons.delegate = self
tableViewButtons.dataSource = self
}
}
extension DropdownViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return carsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let rowData = carsArray[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "buttonCell") as! ButtonTableViewCell
cell.setButton(title: rowData.name)
return cell
}
}