5

PFQueryTableViewControllertableView の相互作用を学習しようとしています。

私が現在持っているもの:tableViewセルはタップで高さを増やします。didSelectRowAtIndexPathまたは、基本的にセル関連のデータを次のviewControllerにシングルタップperformSegueWithIdentifierと一緒に使用できます。prepareForSegue

以下のコードで、「tableView セルの高さを増やすタップ コード」コードをコメントにすると、ダブルタップを検出できます。それ以外の場合は、コードが didSelect ブロック内にあるため、1 回のタップで高さが増加します。

必要なもの:シングルタップで、セルの高さが増減します。セルの高さが増加している間にダブルタップすると、セルデータが次のようにセグエされるはず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")
    }

それを機能させるもう1つの方法は、コードNSIndexPathの外で選択したセルを取得できる場合didSelectRowAtIndexPath、基本的にダブルタップを使用して必要なアクションを取得することです。NSIndexPathtableView のデフォルト ブロックの外に出ることができますか?

4

2 に答える 2

3

うまくいきました!

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

customNSIndexPath = indexPath
        print(customNSIndexPath)

2番目:「コードをタップしてtableViewセルの高さを増やす」コードを削除didSelectRowAtIndexPathし、関数に追加しsingleTap()ます。を使用してセグエを実行し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 ケースで間違っている場合は、コメントしてください。あなたのものがより良いなら、それは本当に他の人を助けるでしょう.

于 2016-01-10T06:46:20.580 に答える
0
var lastClick: TimeInterval = 0.0
var lastIndexPath: IndexPath? = nil

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{
    let now: TimeInterval = Date().timeIntervalSince1970
    if (now - lastClick < 0.3) && (lastIndexPath?.row == indexPath.row ) {
        print("Double Tap!")
    } else {
        print("Single Tap!")
    }
    lastClick = now
    lastIndexPath = indexPath
}
于 2021-05-31T09:22:21.990 に答える