Ios 轻触UILabel(手势识别器),在tableview原型单元中查找一个单元的nil,并在另外两个单元中正常工作
我正在尝试实现UITapGestureRecognizer,其想法是,点击标签,我将得到一个弹出窗口,显示拨打电话的号码,它应该会弹出警告,说call或cancel 我写的代码在3到4个地方对我有用,但有一点我被卡住了 在此屏幕中,我有一个表格视图,其中原型单元格分组类型如下,请检查此图像: 第三个单元格 现在,如果我正在点击065668982我有canOpenURL:URL失败:telprompt://065668982“-错误:“此应用程序不允许查询scheme telprompt”,它实际上可以在iPhone上工作,而不是在模拟器上,并且它可以拨打正常工作的电话 第二个单元格 如果我正在点击0654858我有canOpenURL:URL失败:telprompt://065668982“-错误:“此应用程序不允许查询scheme telprompt”,它实际上可以在iPhone上工作,而不是在模拟器上,并且它可以拨打正常工作的电话 第一个单元格 但对于第一个,它永远不会工作,并最终导致致命错误:在展开可选值时意外发现nil 注意:我从API获取电话号码,并将view controller中的数据附加到UITableViewCell 我希望我有道理,提前感谢任何帮助,如果我不清楚,请在下面评论 这是我的代码:Ios 轻触UILabel(手势识别器),在tableview原型单元中查找一个单元的nil,并在另外两个单元中正常工作,ios,iphone,swift,uitableview,uigesturerecognizer,Ios,Iphone,Swift,Uitableview,Uigesturerecognizer,我正在尝试实现UITapGestureRecognizer,其想法是,点击标签,我将得到一个弹出窗口,显示拨打电话的号码,它应该会弹出警告,说call或cancel 我写的代码在3到4个地方对我有用,但有一点我被卡住了 在此屏幕中,我有一个表格视图,其中原型单元格分组类型如下,请检查此图像: 第三个单元格 现在,如果我正在点击065668982我有canOpenURL:URL失败:telprompt://065668982“-错误:“此应用程序不允许查询scheme telprompt”,它实
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
中的nil。让我假设您使用了故事板并稍微改进了代码
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)
}
}
}
}
为什么不实现
didSelectRowAtIndexPath
delegate而不是在tableview单元格中添加手势?所以我之前尝试了didSelectRowAtIndexPath
,如果我只有一个标签,但对于多个标签(有多个电话号码和电子邮件ID),效果很好我必须使用uitappesturerecognizer
重定向到电话或写电子邮件(我使用此MFMailComposeViewController
发送电子邮件)。感谢@Shadow Of对代码进行更正,我感谢您在我的代码上花费的时间,但是我已经做了这个异常处理我的问题是为什么它会拿走这个东西,这里是我调试它的东西:1)我点击了最后一个数字(表视图中的第三个单元格)我得到了联系点击065668982 2016-06-29 15:27:45.269 ajc[18065:458502]-canOpenURL:URL失败:telprompt://065668982“-错误:“此应用程序不允许查询scheme telprompt”类似地,当我点击我得到的第一个单元格时,它也适用于第二行:contactTapped 065674876 url字符串无效..如果我在字符串中有数字,为什么会出现此异常我运行它并向您显示日志:contactTapped 065668982 2016-06-29 15:27:45.269 ajc[18065:458502]-canOpenURL:URL失败:telprompt://065668982“-错误:“不允许此应用查询scheme telprompt”contactTapped 065454858 2016-06-29 15:27:47.678 ajc[18065:458502]-canOpenURL:URL失败:”telprompt://065454858“-错误:“不允许此应用查询方案提示”“contactTapped 065674876 url字符串无效。。。我希望这对你有用@Sunil看起来您的编号为065674876的字符串在数字的开头或结尾还包含一些空格字符。我编写了touchContact=touchContact。stringByReplacingOccurrencesOfString(“,with string:”)仍然是一样的,没有进展。