[英]On Tap of UILabel (gesture recogniser) finding nil in tableview prototype cell for one cell and its working fine for another two cells
我正在尝试实现UITapGestureRecognizer,想法就是点击标签我会弹出一个显示号码来拨打电话,它应该会出现弹出警报,说出来电或取消!!
我编写的代码在3到4个地方为我工作但我被困在一个点在这个屏幕上我有一个原型单元分组的tableview类型在这里,请检查此图像:
第三细胞
现在,如果我正在点击065668982我有canOpenURL:URL失败:“telprompt:// 065668982” - 错误:“此应用程序不允许查询方案telprompt”这实际上适用于iPhone而不是模拟器上它会调用哪个工作正常。
第二细胞
如果我正在攻击065454858我有canOpenURL:URL失败:“telprompt:// 065668982” - 错误:“此应用程序不允许查询方案telprompt” ,这实际上可以在iPhone上运行而不是在模拟器上,它会调用哪个是工作正常。
第一个细胞
但对于第一个它它永远不会工作并最终导致致命错误:在展开Optional值时意外地发现nil
注意:我从API获取电话号码,并将视图控制器中的数据附加到UITableViewCell。
我希望我有意义,如果我不清楚,请提前感谢任何帮助,请在下面评论
这是我的代码:
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)
}
}
}
问题:1)你应该只添加一次手势2)你应该检查NSURL
为零。 让我假设您使用故事板并稍微改进您的代码
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)
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.