簡體   English   中英

Swift-如果檢測到“滑動刪除”,如何在自定義tableViewCell類中隱藏按鈕?

[英]Swift - How to hide button in custom tableViewCell class if left “Swipe to delete” detected?

我有一個自定義單元格類,其中有2個標簽顯示一個單詞及其翻譯。 當用戶點擊其中一個標簽時,我想打開一個僅顯示標簽內容的新視圖控制器。 我們需要了解點擊了哪個標簽。

我無法使用tapGestureRecognizer和標簽上的標簽使其工作,因為標簽是由類文件管理的。 我發現在標簽頂部添加透明按鈕更加容易。

對新視圖控制器的設置工作正常。 但是我再也不能在tableViewCells上使用“刷卡刪除”了。

當我們向左滑動時,該行向左移動,它在該行的右側顯示紅色正方形的“ DELETE”按鈕,但同時它對新的viewController執行segue。

如果檢測到左側的“滑動刪除”,如何隱藏/停用tableViewCell按鈕?

我試圖添加一個左swipeGestureRecognizer並添加一個公共變量isSwipingLeft: Bool到不同的地方( viewDidEndEditing ... etc),但沒有成功。 我所做的最好的事情是檢測滑動,隱藏按鈕,但是無法取消隱藏按鈕...

編輯:

DetailListCell

protocol CustomCellDelegate {
func leftButtonTapped(cell: DetailListCell)
func rightButtonTapped(cell: DetailListCell)
}

class DetailListCell: UITableViewCell {

@IBOutlet weak var entryLeftLbl: UILabel!
@IBOutlet weak var entryRightLbl: UILabel!
@IBOutlet weak var leftButtonOutlet: UIButton!
@IBOutlet weak var rightButtonOutlet: UIButton!
var delegate: CustomCellDelegate?

func configureDetailListCell(entry: Entry) {
entryLeftLbl.isUserInteractionEnabled = true
entryRightLbl.isUserInteractionEnabled = true
// cell configuration to hide/display labels and buttons ...
}

@IBAction func leftButton(_ sender: Any) {
if isSwipingToDelete == false {
delegate?.leftButtonTapped(cell: self)
}}   

@IBAction func rightButton(_ sender: Any) {
if isSwipingToDelete == false {
delegate?.rightButtonTapped(cell: self)
}}}

DetailListVC

override func viewDidLoad() {
detailTableView.delegate = self
detailTableView.dataSource = self        

// Swipe left
let swipeLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(DetailListVC.swipeLeft(gestureRecognizer:)))
swipeLeft.direction = .left
self.view!.addGestureRecognizer(swipeLeft)
}

// Swipe left to detect Swipe to delete
func swipeLeft(gestureRecognizer: UISwipeGestureRecognizer) {
isSwipingToDelete = true
}

func leftButtonTapped(cell: DetailListCell) {
// Find the row of the textField
let indexPath = self.detailTableView.indexPathForRow(at: cell.center)!        
// Find the Entry object of the row/textField selected
if let objs = controller.fetchedObjects , objs.count > 0 {
let item = objs[indexPath.row].faceA
performSegue(withIdentifier: "FullEntryVC", sender: item)
}}

func rightButtonTapped(cell: DetailListCell) {        
// Find the row of the textField
let indexPath = self.detailTableView.indexPathForRow(at: cell.center)!        
// Find the Entry object of the row/textField selected
if let objs = controller.fetchedObjects , objs.count > 0 {
let item = objs[indexPath.row].faceB
performSegue(withIdentifier: "FullEntryVC", sender: item)
}}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "FullEntryVC" {
if let destination = segue.destination as? FullEntryVC {
if let entryFace = sender as? String {
isEditMode = false
destination.entryLabelValue = entryFace
}}}}

如Valdmer所建議,我使用了UITableViewCell isEditing屬性。 我在按鈕的方法內添加了if語句,以檢查if cell.isEditing == false 如果為false則運行performSegue(withIdentifier: ) ,如果為true則“ performSegue(withIdentifier: )刪除”工作正常,並且該按鈕不執行任何操作。

我刪除了所有與NSNotificationswipeLeft(gestureRecognizer: )相關的代碼。

這是按鈕之一的更新代碼:

func leftButtonTapped(cell: DetailListCell) {
    // Check if "swipe to delete" is detected:
    if cell.isEditing == false {

        // Find the row of the textField
        let indexPath = self.detailTableView.indexPathForRow(at: cell.center)!

        // Find the Entry object of the row/textField selected
        if let objs = controller.fetchedObjects , objs.count > 0 {
            let item = objs[indexPath.row].faceA
            performSegue(withIdentifier: "FullEntryVC", sender: item)
        }
    }
}

暫無
暫無

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

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