簡體   English   中英

長按如何取消選擇UITableViewCell?

[英]How deselect UITableViewCell on long press?

我將UILongPressGesture添加到UITableView並長按選擇單元格。

我已經用該代碼編寫了一些代碼,可以長按選擇該單元格,但是現在我不明白如何在長按時取消選擇,我向您展示我的長按選擇代碼

ViewDidLoad()我編寫了以下代碼

let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress(longPressGesture:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tblListView.addGestureRecognizer(longPressGesture)

這是我的LongPress代碼:

@objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {

    let p = longPressGesture.location(in: self.tblListView)
    let indexPath = self.tblListView.indexPathForRow(at: p)

    if indexPath == nil {
        print("Long press on table view, not row.")
    }
    else if (longPressGesture.state == UIGestureRecognizer.State.began) {
        print("Long press on row, at \(indexPath!.row)")
        let cell = self.tblListView.cellForRow(at: indexPath!) as! GroupDetailTableViewCell
        cell.btnDeleteMember.isHidden = false
    }
}

使用此代碼,我可以選擇單元格,但是現在再次長按,我想取消選擇該單元格,所以我不明白該怎么做。

請告訴我如何解決這個問題。

所以我的問題是如何長按取消選擇單元格,請告訴我該怎么做

謝謝

I think you can want to hide or unhide btnDeleteMember.If so use the following code :

@objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {

    let p = longPressGesture.location(in: self.tblListView)
    let indexPath = self.tblListView.indexPathForRow(at: p)

    if indexPath == nil {
        print("Long press on table view, not row.")
    }
    else if (longPressGesture.state == UIGestureRecognizer.State.began) {
        print("Long press on row, at \(indexPath!.row)")
        let cell = self.tblListView.cellForRow(at: indexPath!) as! GroupDetailTableViewCell
        cell.btnDeleteMember.isHidden = !cell.btnDeleteMember.isHidden
    }

}

創建全局previousIndexPath先前選擇的索引路徑

    //  Global variable
    var previousIndexPath : IndexPath = IndexPath()

    @objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {

        let p = longPressGesture.location(in: self.tblListView)
        let indexPath = self.tblListView.indexPathForRow(at: p)

        if indexPath == nil {
            print("Long press on table view, not row.")
        }
        else if (longPressGesture.state == UIGestureRecognizer.State.began) {
            print("Long press on row, at \(indexPath!.row)")

            // Edited - Add this to reset cell when more than one selected   
            if !previousIndexPath.isEmpty {

               // Reset the Cell
               let cell = self.tblListView.cellForRow(at: previousIndexPath!) as! GroupDetailTableViewCell
               cell.btnDeleteMember.isHidden = true

            }

            let cell = self.tblListView.cellForRow(at: indexPath!) as! GroupDetailTableViewCell
            cell.btnDeleteMember.isHidden = previousIndexPath == indexPath ? 
 true : false // This will make the Select and Deselect 
            previousIndexPath = indexPath
          }
        }

    }

剛打電話

if let selectedIndexPath = self.tblListView.indexPathForSelectedRow {
    self.tblListView.deselectRowAtIndexPath(at: selectedIndexPath, animated: true)
}

取消選擇單元格。

暫無
暫無

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

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