将表格视图单元格变成一个按钮以允许弹出窗口



我正在尝试为表视图单元格制作一个弹出窗口。我想拥有它,以便在按下任何"单元格"时,屏幕上会显示一个弹出窗口。我似乎无法弄清楚如何将每个单元格变成一个按钮,该按钮调用要启动的窗口。

表视图方法

import UIKit
class ContactsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
private let contacts = ContactAPI.getContacts() // model
let contactsTableView = UITableView() // view
var success = true
lazy var popUpWindow: PopUpWindow = {
    let view = PopUpWindow()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.cornerRadius = 5
    view.delegate = self as! PopUpDelegate
    return view
}()
let visualEffectView: UIVisualEffectView = {
    let blurEffect = UIBlurEffect(style: .light)
    let view = UIVisualEffectView(effect: blurEffect)
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()
let button: UIButton = {
    let button = UIButton(type: .system)
    button.backgroundColor = .white
    button.setTitle("one", for: .normal)
    button.setTitleColor(UIColor.blue, for: .normal)
    button.addTarget(self, action: #selector(handleShowPopUp), for: .touchUpInside)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.layer.cornerRadius = 5
    return button
}()
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    view.backgroundColor = .white
    view.addSubview(contactsTableView)
    contactsTableView.translatesAutoresizingMaskIntoConstraints = false
    contactsTableView.topAnchor.constraint(equalTo:view.safeAreaLayoutGuide.topAnchor).isActive = true
    contactsTableView.leftAnchor.constraint(equalTo:view.safeAreaLayoutGuide.leftAnchor).isActive = true
    contactsTableView.rightAnchor.constraint(equalTo:view.safeAreaLayoutGuide.rightAnchor).isActive = true
    contactsTableView.bottomAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    contactsTableView.dataSource = self
    contactsTableView.delegate = self
    contactsTableView.register(ContactTableViewCell.self, forCellReuseIdentifier: "contactCell")
    navigationItem.title = "Contacts"
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return contacts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as! ContactTableViewCell
    cell.contact = contacts[indexPath.row]
    view.addSubview(button)
    button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    button.heightAnchor.constraint(equalToConstant: 50).isActive = true
    button.widthAnchor.constraint(equalToConstant: view.frame.width - 32).isActive = true
    view.addSubview(visualEffectView)
    visualEffectView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    visualEffectView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    visualEffectView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    visualEffectView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    visualEffectView.alpha = 0
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

// MARK: - Selectors
@objc func handleShowPopUp() {
    view.addSubview(popUpWindow)
    popUpWindow.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -40).isActive = true
    popUpWindow.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    popUpWindow.heightAnchor.constraint(equalToConstant: view.frame.width - 64).isActive = true
    popUpWindow.widthAnchor.constraint(equalToConstant: view.frame.width - 64).isActive = true
    popUpWindow.showSuccessMessage = success
    success = !success
    popUpWindow.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    popUpWindow.alpha = 0
    UIView.animate(withDuration: 0.5) {
        self.visualEffectView.alpha = 1
        self.popUpWindow.alpha = 1
        self.popUpWindow.transform = CGAffineTransform.identity
    }
}
}

弹出委托

extension ContactsViewController: PopUpDelegate {
func handleDismissal() {
    UIView.animate(withDuration: 0.5, animations: {
        self.visualEffectView.alpha = 0
        self.popUpWindow.alpha = 0
        self.popUpWindow.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    }) { (_) in
        self.popUpWindow.removeFromSuperview()
        print("Did remove pop up window..")
    }
}
}

UITableViewDelegatedidSelectRowAt方法用于此目的。您可以在那里调用您的handleShowPopUp方法。有什么理由不想使用它吗?

相关内容

最新更新