【问题标题】:PerformSegue from long press on a a custom UiTableViewCell长按自定义 UiTableViewCell 执行Segue
【发布时间】:2020-08-18 07:10:22
【问题描述】:

也许这个问题已经被问了好几次了,但还是找不到。 我是新手。

我有一个带有自定义 UITableViewCell 的 UITableView。 在单元格中有 3 个不同的标签。 我在自定义单元格上添加了手势识别器,这样如果在标签上长按: - 标签 1 应该显示另一个 UiViewController - 标签 2 应该打电话 - 标签 3 应该创建一个邮件

对于标签 2 和 3 我没有问题

但是如何执行 segue 以打开视图控制器?

这是故事板

这是自定义的tableviewcell

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”类型的值没有成员“performSegue”

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

如何实现我所需要的?

提前致谢 法布里齐奥

【问题讨论】:

  • 请写下您的 TableView 持有类的代码,您需要实现委托并从主类执行 segue,因为单元类无法执行 segue ...我将指导您如何创建协议委托来执行在您发布创建此单元格的主类的代码后,从主类中继续
  • 解决了您的问题先生?

标签: swift uitableview segue


【解决方案1】:

您需要实现委托并从主类执行 segue,因为单元类无法执行 segue ... in storyBoard attach "moveToMaps" segue 从主控制器

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

【讨论】:

    猜你喜欢
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多