gpt4 book ai didi

swift - 将 Tableview Cell 制作成按钮以允许弹出窗口

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

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

表格 View 方法

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

}

弹出委托(delegate)

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 方法。您有什么理由不想使用它吗?

关于swift - 将 Tableview Cell 制作成按钮以允许弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56731496/

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