简体   繁体   中英

On Tap of UILabel (gesture recogniser) finding nil in tableview prototype cell for one cell and its working fine for another two cells

I am trying to implement UITapGestureRecognizer, Idea is that on tap of label I will get a pop up displaying the number to make call and it should come up with pop up alert saying call or cancel!!

Code I've written worked for me in 3 to 4 places but I am stuck at one point In this screen I have a tableview with prototype cells grouped type here, please check this Image:

Link For Image

Third Cell

Now If I am Tapping 065668982 I have canOpenURL: failed for URL: "telprompt://065668982" - error: "This app is not allowed to query for scheme telprompt" which actually works on iPhone not on simulator and it pulls to call which is working fine.

Second Cell

If I am Tapping 065454858 I have canOpenURL: failed for URL: "telprompt://065668982" - error: "This app is not allowed to query for scheme telprompt" which actually works on iPhone not on simulator and it pulls to call which is working fine.

first Cell

But for first one it never works and end up with fatal error: unexpectedly found nil while unwrapping an Optional value

NOTE : I am Getting phone Number from an API and append the data in view controller to UITableViewCell.

I Hope I make sense, Thanks in advance for any help also if I am not clear please comment below

Here is my code:

import UIKit
import Foundation

class XyzTableViewCell: UITableViewCell
{
    @IBOutlet weak var phoneNumber: UILabel!
    var touchContact : String = ""

   var myCell: MyCellData! {
        didSet {
            self.updateUI()
        }
    }

    func updateUI()
    {
        touchContact = vicarCell.phone_no

        //Tap Gesture
        tapGestureAddonView()
    }

    //MARK:- Tap to Call and Open Email
    func tapGestureAddonView(){

        let contacttap = UITapGestureRecognizer(target: self, action:("contactTapped"))
        contacttap.numberOfTapsRequired = 1
        phoneNumber!.userInteractionEnabled = true
        phoneNumber!.addGestureRecognizer(contacttap)


    }

    func contactTapped() {
        // do something cool here

        print("contactTapped")
        print(touchContact)

        dispatch_async(dispatch_get_main_queue())
        {
        if UIApplication.sharedApplication().canOpenURL(NSURL(string: "telprompt://\(self.touchContact)")!){

            UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://\(self.touchContact)")!)
        }
        else{
            //showAlert("Info",message: "Your device could not called" ,owner: self)
        }
        }
    }

The issues: 1) you should add gesture only once 2) you should check NSURL for nil. Let me assume that you use storyboard and improve your code a bit

class XyzTableViewCell: UITableViewCell
{
    @IBOutlet weak var phoneNumber: UILabel!
    var touchContact : String = ""

    var myCell: MyCellData! {didSet {self.updateUI()}}

    func updateUI() {
        touchContact = myCell.phone_no // did you mean myCell instead vicarCell?
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        tapGestureAddonView()
    }

    func tapGestureAddonView(){
        let contacttap = UITapGestureRecognizer(target: self, action: #selector(contactTapped))
        contacttap.numberOfTapsRequired = 1
        phoneNumber!.userInteractionEnabled = true
        phoneNumber!.addGestureRecognizer(contacttap)
    }

    func contactTapped() {
        print("contactTapped")
        print(touchContact)
        if touchContact.isEmpty {return}
        guard let url = NSURL(string: "telprompt://\(touchContact)") else {
            print("url string invalid")
            return
        }
        dispatch_async(dispatch_get_main_queue())
        {
            if UIApplication.sharedApplication().canOpenURL(url){
                UIApplication.sharedApplication().openURL(url)
            } else{
                //showAlert("Info",message: "Your device could not called" ,owner: self)
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM