Сделайте Tableview Cell кнопкой, чтобы разрешить всплывающее окно - PullRequest
0 голосов
/ 24 июня 2019

Я пытаюсь сделать всплывающее окно для ячейки таблицы.Мне бы хотелось, чтобы при нажатии любой из «ячеек» на экране отображалось всплывающее окно.Я не могу понять, как превратить каждую ячейку в кнопку, которая вызывает окно, которое должно быть инициировано.

Методы табличного представления

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..")
    }
}

}

1 Ответ

0 голосов
/ 24 июня 2019
Для этого используется метод

UITableViewDelegate didSelectRowAt.Вы можете просто вызвать свой handleShowPopUp метод там.Есть ли причина, по которой вы не хотите его использовать?

...