5

私はPFQueryTableViewControllerを持っています。この中で、いくつかのtableViewインタラクションを学習しようとしています。 tableView細胞はタップdidSelectRowAtIndexPathにその高さを増大させたり、私は基本的にシングルタップを使用してprepareForSegueと一緒performSegueWithIdentifierを使用して、次のViewControllerに細胞関連データをセグエことができます。私は現在持っている何2つの操作を実行するためにUITableViewCellでダブルタップとシングルタップを検出

以下のコードで、「TabViewコードの高さを上げるタップコード」コードにコメントすると、ダブルタップを検出できます。コードが内側にあるので、他の賢明なタップは高さを増やします。選択ブロック。

必要なもの: シングルタップでは、セルの高さが増減します。セルの高さは増加しますが、ダブルタップするとセルのデータは次のUIViewControllerにセグメンテーションされます。

もしそうでなければ、シングルタップは高さを増減する必要があります。そして、ダブルタップは、次のUIViewController

コードにセルデータをセグエ必要があります以下の通りです:

var selectedCellIndexPath: NSIndexPath? 
var SelectedCellHeight = CGFloat() // currently set to 480.0 tableView height 
var UnselectedCellHeight = CGFloat() // currently set to 300.0 tableView unselected hight 
    .... 

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if let selectedCellIndexPath = selectedCellIndexPath { 
     if selectedCellIndexPath == indexPath { 
      return SelectedCellHeight 
     } 
    } 
    return UnselectedCellHeight 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! DataViewTableViewCell! 
    if cell == nil { 
     cell = DataViewTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") 
    } 

// Below is the double Tap gesture recognizer code in which 
// The single tap is working, double tap is quite difficult to get 
// as the cell height increases on single tap of the cell 

    let doubleTap = UITapGestureRecognizer() 
     doubleTap.numberOfTapsRequired = 2 
     doubleTap.numberOfTouchesRequired = 1 
     tableView.addGestureRecognizer(doubleTap) 

     let singleTap = UITapGestureRecognizer() 
     singleTap.numberOfTapsRequired = 1 
     singleTap.numberOfTouchesRequired = 1 
     singleTap.requireGestureRecognizerToFail(doubleTap) 
     tableView.addGestureRecognizer(singleTap) 

// Tap code to increases height of tableView cell 

    if let selectedCellIndexPath = selectedCellIndexPath { 
     if selectedCellIndexPath == indexPath { 
      self.selectedCellIndexPath = nil 
      cell.postComment.fadeIn() 
      cell.postCreatorPicture.alpha = 1 
      cell.postDate.fadeIn() 
      // cell.postTime.fadeIn() 
      cell.postCreator.fadeIn() 
     } else { 
      self.selectedCellIndexPath = indexPath 
     } 
    } else { 
     selectedCellIndexPath = indexPath 
    } 
    tableView.beginUpdates() 
    tableView.endUpdates() 
} 



func doubleTap() { 
     print("Double Tap") 
    } 

func singleTap() { 
     print("Single Tap") 
    } 

私は、didSelectRowAtIndexPathコードの外に選択したセルのNSIndexPathを得ることができる場合、私はそれを動作させることができ、他の方法は、あります基本的にダブルタップを使用して、必要なアクションを得るために他の操作を実行できます。テーブルビューのデフォルトブロックの外にNSIndexPathを取得できますか?

+0

デリゲートを使用してセルオブジェクトを渡すことができます。 NSIndexPath * indexPath = [self.tableView indexPathForCell:cell]; – Mohamad

答えて

3

最初に、var customNSIndexPath = NSIndexPath()を定義し、didSelectRowAtIndexPathコードブロック内に以下のコードを追加します。

customNSIndexPath = indexPath 
     print(customNSIndexPath) 

第二:didSelectRowAtIndexPathからコードとsingleTap()機能に追加「のtableViewセルの高さを高くするには、をタップコード」を削除します。 doubleTap()を使用してセグを実行します。

func doubleTap() { 
     print("Double Tap") 
     performSegueWithIdentifier("MainToPost", sender: self) 
    } 

    func singleTap() { 
     print("Single Tap") 
     var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! DataViewTableViewCell! 
     if cell == nil { 
      cell = DataViewTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") 
     } 

     if let selectedCellIndexPath = selectedCellIndexPath { 
      if selectedCellIndexPath == customNSIndexPath { 
       self.selectedCellIndexPath = nil 

      } else { 
       self.selectedCellIndexPath = customNSIndexPath 
      } 
     } else { 
      selectedCellIndexPath = customNSIndexPath 
     } 
     tableView.beginUpdates() 
     tableView.endUpdates() 
    } 

これは、あなたが眠らないで遅く動作する方法です。それは最も簡単なことでした。ありがとう

注:誰かがより良い解決法を持っているか、これがxyzの場合に間違っている場合は、コメントをしてください。もしあなたの方が良ければ、本当に他人を助けるでしょう。

関連する問題