2016-06-08 76 views
2

我想要在編輯其中的內容的UITableView中的每個單元格中添加點按手勢。添加手勢的兩種方式是使用代碼或通過故事板。我試了兩次,但都失敗了。如何向UITableViewCell添加手勢?

我可以添加一個手勢每個單元格與故事板拖放?它似乎只爲第一個單元添加手勢。在代碼中添加的姿態,我寫的東西一樣,

addGestureRecognizer(UITapGestureRecognizer(target: self,action:#selector(MyTableViewCell.tapEdit(_:)))) 

addGestureRecognizer(UITapGestureRecognizer(target: self, action:"tapEdit:")) 

都工作。但我想讓UITableViewController處理這個手勢,因爲它對數據源做了一些事情。我如何編寫我的目標和行動?

編輯:

 addGestureRecognizer(UITapGestureRecognizer(target: MasterTableViewController.self, action:#selector(MasterTableViewController.newTapEdit(_:))) 

它引起的錯誤說,無法識別的選擇發送到0x106e674e0類...

+0

使用每個按鈕的唯一標籤號在UITableViewCell上添加UIButton。指定一個方法到按鈕。您可以在該方法上獲得標籤號碼 – kb920

回答

17

要添加姿態的UITableViewCell,你可以按照下面的步驟:

第一,向UITableView添加手勢識別器

tapGesture = UITapGestureRecognizer(target: self, action: #selector(tableViewController.tapEdit(_:))) 
tableView.addGestureRecognizer(tapGesture!) 
tapGesture!.delegate = self 

然後,define選擇器。使用recognizer.locationInView來定位您在tableView中點擊的單元格。你可以通過tapIndexPath訪問dataSource中的數據,這是用戶點擊的單元格的indexPath。

func tapEdit(recognizer: UITapGestureRecognizer) { 
    if recognizer.state == UIGestureRecognizerState.Ended { 
     let tapLocation = recognizer.locationInView(self.tableView) 
     if let tapIndexPath = self.tableView.indexPathForRowAtPoint(tapLocation) { 
      if let tappedCell = self.tableView.cellForRowAtIndexPath(tapIndexPath) as? MyTableViewCell { 
       //do what you want to cell here 

      } 
     } 
    } 
} 

是可能的姿態直接添加到TableView中細胞和訪問的viewController數據源,您需要設置代理:

在您的自定義單元格:

import UIKit 


class MyTableViewCell: UITableViewCell { 

    var delegate: myTableDelegate? 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     let tapGesture = UITapGestureRecognizer(target: self, action: #selector(MyTableViewCell.tapEdit(_:))) 
     addGestureRecognizer(tapGesture) 
     //tapGesture.delegate = ViewController() 

    } 

    func tapEdit(sender: UITapGestureRecognizer) { 
     delegate?.myTableDelegate() 
    } 

} 

protocol myTableDelegate { 
    func myTableDelegate() 
} 

在您的viewController中:

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate, myTableDelegate { 

    @IBOutlet var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.delegate = self 
     tableView.dataSource = self 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 35 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? MyTableViewCell 

     cell?.delegate = self 

     return cell! 
    } 

    func myTableDelegate() { 
     print("tapped") 
     //modify your datasource here 
    } 

} 

但是,此方法可能會導致問題,請參閱UIGestureRecognizer and UITableViewCell issue。在這種情況下,當滑動手勢成功時,選擇器因某種原因被調用兩次。我不能說第二種方法是不好的,因爲我還沒有找到任何直接的證據,但在搜索Google之後,似乎第一種方法是標準方法。

+0

向tableViewCell添加手勢而不是tableView然後指定代碼中的哪一個似乎很自然。而對於更復雜的手勢(如平移),寫入條件會檢查平底鍋是否開始/停留/結束於單元格中,並處理其他情況對我來說可能會很痛苦。我想給tableviewcell添加手勢,在tableviewcell.swift中編寫代碼。可能嗎?你可以回答我的第二個問題,我該如何編寫UITapGestureRecognizer的目標和選擇器(目標,動作) –

+0

請看我的更新回答 – luiyezheng

+0

目標和選擇器'let tapGesture = UITapGestureRecognizer(target:self,action:#selector(MyTableViewCell。 tapEdit(_ :)))' – luiyezheng

4

你不需要添加手勢識別器來實現你正在做的事情。

  • 使用UITableViewDelegate方法tableView:didSelectRowAtIndexPath:檢測哪一行被竊聽(這到底是什麼你tapGesture會做),然後做你所需的處理。恢復單元前
    cell?.selectionStyle = .None
+0

點擊只是一個例子,我很想做各種手勢在每個細胞上。 –

+0

你有沒有自定義的單元格類? –

+0

如果是,在自定義單元類 –

-1

在的cellForRowAtIndexPath,:

  • 如果當您選擇單元格不喜歡灰色的指示,在您的tableView:didEndDisplayingCell:forRowAtIndexPath:剛剛返回細胞先鍵入此。創建點按手勢並設置爲單元格。

  • 相關問題