我正在使用Xcode 7 Beta,Swift 2 我有一個帶有自定義tableViewCell的表視圖。在自定義單元格中有一個UIImageView和Label。我也爲這個tableViewCell定義了一個testCell類。在圖像上(表格內),我添加了UITapRecogniser。我也啓用了用戶交互iOS - 自定義TableViewCell中的TapRecogniser無法正常工作
問題:目前我在表中有3行。當我點擊前兩行的圖片時。什麼都沒發生。當我點擊最後一行的圖像時 - 操作在控制檯上打印「DEF」。這與行數沒有任何關係 - 即使在更改爲4,5之後,問題仍然存在。基本上只有最後一行的圖像被挖掘。不知道爲什麼?以下是代碼:
//Defining custom class for the TableViewCell
class testCell : UITableViewCell{
@IBOutlet weak var testLabel: UILabel!
@IBOutlet weak var testImage: UIImageView!
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! testCell
cell.testLabel?.text = "ABC"
cell.testImage?.image = UIImage(named: "Santa")
return cell
}
@IBOutlet weak var tableView: UITableView!
//TapGestureRecogniser Function
@IBAction func imageTap(sender: AnyObject) {
print("DEF")
}
將添加手勢識別器調用從testCell移動到cellforrowatindexpath。讓cell = tableView.dequeueReusableCellWithIdentifier(「cell」,forIndexPath:indexPath)as! testCell cell.testLabel的.text = 「ABC」 cell.testImage =圖像配的UIImage(命名爲: 「聖誕老人」)? 讓cellTapRecognizer = UITapGestureRecognizer(目標:自我,動作:選擇( 「handleTap:」)) cellTapRecognizer .cancelsTouchesInView = false cell.testImage?.addGestureRecognizer(cellTapRecognizer) –