从长按自定义的UiTableViewCell上执行转场。

问题描述 投票:0回答:1

也许这个问题已经被问过好几次了,但是找不到了,我是个新手,我有一个UITableView,里面有一个自定义的UITableViewCell.在这个单元格里有3个不同的标签。

我有一个UITableView,有一个自定义的UITableViewCell.在单元格中有3个不同的标签,我在自定义单元格中添加了手势识别器,所以如果长按标签:-标签1应该显示另一个UiViewController-标签2应该做一个调用-标签3应该创建一个邮件。

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

但如何执行转场来打开视图控制器?

这是故事板

Storyboard

这是自定义的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'。

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

如何实现我需要什么?

先谢谢你Fabrizio

swift uitableview segue
1个回答
0
投票

你需要实现委托,并从主类中执行转接,因为单元格类不能执行转接......。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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.