I would like to pass a function as parameter, and use it in the UITableView extension. In the function named 'setEmptyView', I made UIButton programmatically, and added some target action. Here's what I've done so far.
extension UITableView {
func setEmptyView(message: String, actionButtonClosure: @escaping () -> Void) {
let emptyView = UIView()
let messageLabel = UILabel()
let button = UIButton()
// adding subview and making constraints codes....
button.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
}
@objc func buttonAction (_ sender: UIButton!) {
actionButtonClosure() <---- error
}
}
class ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if count == 0 {
let message = "Here is placeholder for this table"
tableView.setEmptyView(message: message, buttonTitle: "see more") { [self] in
goToMenu()
}
}
return count
}
func goToMenu() {
// move to next VC
}
Unfortunately 'actionButtonClosure' function in parameter cannot be accessible because it's two different function. I also tried .addAction() to the button, but it is only available since iOS14. What would you suggest to do here? How can I pass function parameter and use it as button action?