簡體   English   中英

將Tap手勢識別器添加到TableViewCell.swift

[英]Adding a Tap Gesture Recognizer to TableViewCell.swift

我有一個表格視圖單元格,其中帶有一個圖像,該圖像已附加了Tap Gesture Recognizer。

表視圖中原型TableViewCell的圖像,帶有兩個標簽和一個圖像。 頂部是Tap Gesture Recognizer徽標:

在此處輸入圖片說明

當我嘗試ctrl並將Tap Gesture Recognizer拖動到TableViewCellControllerTableViewCell文件或任何其他文件時,它不會讓我使用。

但是,在不是表視圖的其他視圖上,我可以使其正常運行。 我是Swift的新手。 有誰知道這是為什么發生的,以及我可以做些什么來幫助它(我知道我可以改用按鈕,但是我從中學不到任何東西)?

TableViewCellTableViewCellController文件都有其默認代碼。

編輯 :我想要的是用戶能夠點擊圖像以將+1添加到表單元格中的類的Int屬性。 類和單元中的UILabel之一已更新。 我可以在常規視圖中毫無問題地實現這種行為,並且希望在表視圖中實現。

對於UITableViewCell您無法通過情節UITapgesture向其添加UITapgesture ,因為它只是一個原型單元,與其他單元格不同。 如果您確實想這樣做,可以在委托中以編程方式進行,如下所示:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    let tapGesture = UITapGestureRecognizer(target: self, action: "SomeMethod")
    cell.myImageView.addGestureRecognizer(tapGesture)

    return cell
}

但是我找不到你為什么要這么做。 如果您只想在點擊單元格時執行某項操作或調用某些函數,則應使用didSelectRowAtIndexPath

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("cell at #\(indexPath.row) is selected!")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM