在表视图单元格中长按时传递数据

时间:2019-11-03 06:52:24

标签: swift xcode tableview

有一个用于显示电话联系的表格视图。长按单元格时,我想将电话号码和电子邮件发送给另一个View Controller。长按可以正常工作,但是我无法将数据传递给另一个View Controller。

enter image description here

VC 1:

  override func viewDidLoad() {
        super.viewDidLoad()

        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longpress))
        tbMain.addGestureRecognizer(longPress)
}

表格单元格的长按方法:

  @objc func longpress(sender: UILongPressGestureRecognizer) {

        if sender.state == UIGestureRecognizer.State.began {
            let touchPoint = sender.location(in: tbMain)
            if tbMain.indexPathForRow(at: touchPoint) != nil {

                let cell = tbMain.dequeueReusableCell(withIdentifier: "testCell") as! NewContactCell

                print("Long press Pressed:)")

                self.actionVC = self.storyboard!.instantiateViewController(withIdentifier: "ActionsViewController") as? ActionsViewController

                UIView.transition(with: self.view, duration: 0.25, options: [.transitionCrossDissolve], animations: {
                    self.view.addSubview( self.actionVC.view)
                }, completion: nil)


            }
        }
    }

VC 2:

 internal var strPhoneNUmber : String!
 internal var strEmail : String!

    override func viewDidLoad() {
        super.viewDidLoad()
        print("Phone: \(strPhoneNUmber!)")
        // Do any additional setup after loading the view.
    }

2 个答案:

答案 0 :(得分:0)

从单元格对象获取电话号码和电子邮件

self.actionVC.strPhoneNUmber = cell.strPhoneNUmber // get phone number from cell object 
self.actionVC. strEmail = cell.strEmail // get email from cell object 

代码就像

@objc func longpress(sender: UILongPressGestureRecognizer) {
    if sender.state == UIGestureRecognizer.State.began {
        let touchPoint = sender.location(in: tbMain)
        if tbMain.indexPathForRow(at: touchPoint) != nil {

            let cell = tbMain.dequeueReusableCell(withIdentifier: "testCell") as! NewContactCell

            print("Long press Pressed:)")

            self.actionVC = self.storyboard!.instantiateViewController(withIdentifier: "ActionsViewController") as? ActionsViewController
            **self.actionVC.strPhoneNUmber = cell.strPhoneNUmber // get phone number from cell object 
            self.actionVC. strEmail = cell.strEmail // get email from cell object**
            UIView.transition(with: self.view, duration: 0.25, options: [.transitionCrossDissolve], animations: {
                self.view.addSubview( self.actionVC.view)
            }, completion: nil)


        }
    }
}

答案 1 :(得分:0)

我看不到您何时尝试传递数据。.您有很多方法可以首先执行该操作,然后可以使用委托来实现传递数据

protocol YourDelegate : class { 
func passData(phoneNumber: String, email: String)
}
weak var delegate: YourDelegate?


@objc func longpress(sender: UILongPressGestureRecognizer) {

    if sender.state == UIGestureRecognizer.State.began {
        let touchPoint = sender.location(in: tbMain)
        if tbMain.indexPathForRow(at: touchPoint) != nil {

            let cell = tbMain.dequeueReusableCell(withIdentifier: "testCell") as! NewContactCell

            print("Long press Pressed:)")



            self.actionVC = self.storyboard!.instantiateViewController(withIdentifier: "ActionsViewController") as? ActionsViewController
self.delegate = self
**delegate?.passData(cell.strPhoneNumber, cell.strEmail)**

            UIView.transition(with: self.view, duration: 0.25, options: [.transitionCrossDissolve], animations: {
                self.view.addSubview( self.actionVC.view)
            }, completion: nil)


        }
    }
}



class ???: UIViewController, YourDelegate {
 var strPhoneNUmber : String!
 var strEmail : String!

override func viewDidLoad() {
    super.viewDidLoad()
    print("Phone: \(strPhoneNUmber!)")
    // Do any additional setup after loading the view.
}

func passData(phoneNumber: String, email: String) {
handle...
}
}

我不清楚是否要将actionVC传递给该数据,但是如果有,那么就拥有一个实例。.只是设置属性,但仍然建议坚持使用委托模式

actionVC.strPhoneNumber = cell.strPhoneNumber

或使用segue

    self.performSegue(withIdentifier: "yourIdentifier", sender: arr[indexPath.row])

使用prepare for segue创建一个实例,以根据发送者设置其属性。

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destvc = segue.destination as? YourClass
        destvc.phoneNumber = sender.phoneNumber as? String
        destvc.email = sender.email as? String

    }