gpt4 book ai didi

ios - 关闭 TableViewCell 的 UIViewController

转载 作者:行者123 更新时间:2023-11-28 07:27:13 25 4
gpt4 key购买 nike

我在 UIViewController 中有一个 TableView 。这个表格 View 有一个带按钮的动态表格。

我像这样为 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")
}

}

我如何关闭函数 @objc func changeValue(_ sender: Any) 中包含 TableView 的 UIViewController?

此外,由于按钮是动态添加的,我确实还需要 tableview 本身的动态高度 - 如何调整 tableview 的每个添加的单元格?

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
}
}

最佳答案

您可以使用委托(delegate):

protocol ButtonTableViewCellDelegate {
func dismissFromCell()
}

class ButtonTableViewCell: UITableViewCell {

let defaults = UserDefaults.standard
var targetTitle: String!

// Delegate should be weak to avoid memory leaks
weak var delegate: ButtonTableViewCellDelegate?

@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")
delegate?.dismissFromCell()
}
}

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)
// Setting Delegate here
cell.delegate = self
return cell
}
}

// Extend your controller to conform your Delegate Protocol
extension DropdownViewController: ButtonTableViewCellDelegate {
func dismissFromCell() {
self.dismiss()
}
}

您可以在此处获得有关委托(delegate)模式的更多信息:https://www.appcoda.com/swift-delegate/

关于ios - 关闭 TableViewCell 的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56192857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com