我是iOS開發新手,使用xcode 8.2.1 & swift 3.我在視圖控制器中使用uitableview,在uitableviewcell中使用uiimageview當我點擊它時,應用程序崩潰並且當我採取按鈕並執行按鈕操作時,會發生同樣的問題。當我點擊uitableviewcell中的uiimageview時,應用程序崩潰
錯誤是:
sampleToRunBuild [3752:1620857] - [sampleToRunBuild.TapViewController TappedOnImage:]:無法識別的選擇發送到實例0x141e07bc0 2017年4月13日18:18:00.531126 sampleToRunBuild [3752 :1620857] * 終止應用程序由於未捕獲的異常 'NSInvalidArgumentException' 的,理由是: ' - [sampleToRunBuild.TapViewController TappedOnImage:]:無法識別 選擇發送到實例0x141e07bc0' *第一擲通話堆棧:(0x18343d1b8 0x181e7455c 0x183444268 0x183441270 0x18333a80c 0x1898b3f80 0x1898b7688 0x18947e73c 0x18931d0f0 0x1898a7680 0x1898a71e0 0x1898a649c 0x18931b30c 0x1892ebda0 0x189ad575c 0x189acf130 0x1833eab5c 0x1833ea4a4 0x1833e80a4 0x1833162b8 0x184dca198 0x1893567fc 0x189351534 0x100065f30 0x1822f95b8)的libC++ abi.dylib:與未捕獲 異常類型的終止NSException
我的代碼是:
import UIKit
class TapViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mycell")
let img: UIImageView = cell?.viewWithTag(1) as! UIImageView
let img2: UIImageView = cell?.viewWithTag(2) as! UIImageView
img.tag = indexPath.row
img2.tag = indexPath.row
img.isUserInteractionEnabled = true
img2.isUserInteractionEnabled = true
let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("TappedOnImage:")))
tapped.numberOfTapsRequired = 1
tapped.delegate = self
img.addGestureRecognizer(tapped)
let tapped1:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("TappedOnImage:")))
tapped1.numberOfTapsRequired = 1
tapped1.delegate = self
img.addGestureRecognizer(tapped1)
return cell!
}
func TappedOnImage(sender:UITapGestureRecognizer){
print("tap on imageview")
}
}
在此先感謝...
謝謝你這是我的工作 –
歡迎你回覆我的回答@KunalMalve –