PerformSegue от долгого нажатия на пользовательский UiTableViewCell - PullRequest
0 голосов
/ 04 мая 2020

возможно вопрос задавался несколько раз, но не могу его найти. Я новичок ie.

У меня есть UITableView с пользовательским UITableViewCell. в ячейке есть 3 разных ярлыка. Я добавил распознаватель жестов в пользовательскую ячейку, чтобы при длительном нажатии на метку: - метка 1 должна показывать другой UiViewController - метка 2 должна выполнять вызов - метка 3 должна создавать почту

для меток 2 и 3 у меня не было проблем

но как выполнить переход, чтобы открыть контроллер представления?

Это раскадровка

Storyboard

Это пользовательская ячейка просмотра таблицы

import UIKit
import MessageUI

class OfficeCell: UITableViewCell, MFMailComposeViewControllerDelegate {

    @IBOutlet weak var lbOffice: UILabel!
    @IBOutlet weak var lbAddress: UILabel!
    @IBOutlet weak var lbPhone: UILabel!
    @IBOutlet weak var lbMail: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.setupLabelTap()
        // Configure the view for the selected state
    }

    func setupLabelTap() {

        let lbAddressTap = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressReconizer(_:)))
        self.lbAddress.isUserInteractionEnabled = true
        self.lbAddress.addGestureRecognizer(lbAddressTap)

        let lbAddressTap2 = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressReconizer(_:)))
        self.lbMail.isUserInteractionEnabled = true
        self.lbMail.addGestureRecognizer(lbAddressTap2)

        let lbAddressTap3 = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressReconizer(_:)))
        self.lbPhone.isUserInteractionEnabled = true
        self.lbPhone.addGestureRecognizer(lbAddressTap3)
    }

    @objc func longPressReconizer(_ sender: UITapGestureRecognizer) {
        print("labelTapped")
        let etichetta :UILabel = sender.view as! UILabel
        print (etichetta.text!)
        switch etichetta.tag {
        case 0:

            self.performSegue(withIdentifier: "moveToMaps", sender: self)



        case 1:
            let telNumber = etichetta.text!.replacingOccurrences(of: " ", with: "")
            if let phoneCallURL = URL(string: "telprompt://\(telNumber)") {
                let application:UIApplication = UIApplication.shared
                if (application.canOpenURL(phoneCallURL)) {
                    application.open(phoneCallURL, options: [:], completionHandler: nil)
                }
            }
        case 2:

            if MFMailComposeViewController.canSendMail() {
                let mail = MFMailComposeViewController()
                mail.mailComposeDelegate = self
                mail.setToRecipients([etichetta.text!])
                mail.setSubject("Informazioni")

                self.window?.rootViewController?.present(mail, animated: true)
            } else {
                // show failure alert
            }

        default:
            print("default")
        }
    }

    func mailComposeController(_ controller:MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }
}

, но Xcode выдает мне эту ошибку

Значение типа 'OfficeCell' не имеет члена 'executeSegue'

на self.performSegue (withIdentifier: "moveToMaps", отправитель: self)

как добиться того, что мне нужно?

Заранее спасибо Fabrizio

1 Ответ

0 голосов
/ 04 мая 2020

вам нужно реализовать делегат и выполнить переход из основного класса, потому что класс ячеек не может выполнить переход ... в storyBoard присоединить "moveToMaps" переход из основного контроллера

protocol CellDelegate {
  func didTapAddressButton()
}

class OfficeCell: UITableViewCell, MFMailComposeViewControllerDelegate {
var delegate: CellDelegate?
}

В вашем основном представлении класс контроллера

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OfficeCell", for: indexPath) as! OfficeCell
cell.delegate = self 
return cell 
}

extension MainController: CellDelegate {
   func didTapAddressButton(){
     self.performSegue(withIdentifier: "moveToMaps", sender: self)
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...