簡體   English   中英

TableView 滑動刪除:如何以編程方式取消滑動

[英]TableView Swipe to Delete: How to Programmatically Un-swipe

我正在使用表格視圖中的滑動刪除功能來刪除選定的單元格。 您將其向左滑動以顯示刪除按鈕。 我的代碼運行良好,可以正常刪除。

我還包含了一個 UIAlertController,為應用程序的用戶提供最后一次避免出錯的機會。 我想要做的是,如果用戶選擇“取消”,則讓單元格“取消滑動”並返回其原始位置。 有沒有辦法做到這一點?

您可以關閉 tableView 上的編輯

[self.tableView setEditing:NO animated:YES];

我遇到了這個問題,試圖做同樣的事情。 我認為必須有一個更好的解決方案來重新加載表,所以我做了一些進一步的研究並發現了以下內容。

tableView.setEditing(false, animated: true)

這里有更多代碼(Swift 3)來顯示它的使用情況

    // Custom cell edit actions
func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
    let delete = UITableViewRowAction(style: .destructive, title: "Delete") {(rowAction: UITableViewRowAction, indexPath: IndexPath) -> Void in

        let refreshAlert = UIAlertController(title: "Delete", message: "Are you sure you want to delete this item.", preferredStyle: UIAlertControllerStyle.alert)

        refreshAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
            let context = self.fetchedResultsController.managedObjectContext
            context.delete(self.fetchedResultsController.object(at: indexPath))

            do {
                try context.save()
            } catch {
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }))

        refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
            // if the user presses cancel, slide the cell back
            tableView.setEditing(false, animated: true)
        }))

        self.present(refreshAlert, animated: true, completion: nil)

    }

    let editAction = UITableViewRowAction(style: .normal, title: "Edit") { action, index in
        print("edit button tapped")
    }

    editAction.backgroundColor = .orange


    return [editAction, delete]
}

我有一個類似的問題,發現你的問題正在尋找答案。 基於這里缺乏答案,我認為我必須自己弄清楚。 這是我的問題以及我如何解決它。 我希望它對你有用,因為我解決了它非常簡單。

我有一個帶有開始屏幕的應用程序,該屏幕顯示一個包含多個軌道的表格。 當您選擇一條軌跡時,您可以顯示軌跡詳細信息、在地圖上顯示軌跡(使用 MapView)或刪除軌跡。

在此處輸入圖片說明

但是,當您第一次使用該應用程序時,表格是空的,並顯示一個表格單元格:“沒有保存的曲目”。

在此處輸入圖片說明

這意味着該表為空,您無法顯示曲目詳細信息、曲目地圖或刪除曲目。 我想確保如果桌子是空的,你會收到一條不同的消息,並且向左滑動被撤消。

在此處輸入圖片說明

代碼是 funk -tableView:editActionsForRowAtIndexPath indexPath 的過度:

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    let selectedRow = indexPath.row

    guard self.savedTracks.count > 0 else {
    //There are no saved tracks, the action cannot be executed.
        let faultAction = UITableViewRowAction(style: .Normal, title: "No tracks", handler: { (action, indexPath) -> Void in

            self.trackOverviewTable.reloadData() //Simply reload the table view after the action button is hit.
        })

        return [faultAction]
    }

    //Show track details
    let detailsAction = UITableViewRowAction(style: .Default, title: "Details") { (action, indexPath) -> Void in

        print("I now selected the track details view.\n")
        self.newTrack = self.savedTracks[selectedRow]
        self.performSegueWithIdentifier("showTrackDetails", sender: self)
    }

    //Show track map
    let mapAction = UITableViewRowAction(style: .Normal, title: "Map") { (action, indexPath) -> Void in

        print("I now selexted the track map view.\n")
        self.newTrack = self.savedTracks[selectedRow]
        self.performSegueWithIdentifier("showTrackMap", sender: self)
    }

    //Delete the selected track
    let deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) -> Void in

        print("I now pressed the delete button.\n")
        self.savedTracks.removeAtIndex(selectedRow)
        if self.savedTracks.count > 0 {
            self.trackOverviewTable.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else {
            self.trackOverviewTable.reloadData()
        }
    }

    detailsAction.backgroundColor = UIColor(colorLiteralRed: 21.0/255.0, green: 175.0/255.0, blue: 253.0/255.0, alpha: 0.8)
    mapAction.backgroundColor = UIColor(colorLiteralRed: 37.0/255.0, green: 155.0/255.0, blue: 253.0/255.0, alpha: 0.8)

    return [deleteAction, mapAction, detailsAction]
}

這里重要的部分是“guard else”語句,當沒有軌道保存時,代碼會在這里執行。 基本上,swipi 會顯示一條不同的消息“無軌道”,當單擊此操作按鈕時,將重新加載表格並取消滑動。 由於您使用 UIAlertController,因此適用相同的原則。 上述方法的工作原理與 UIAlertController 幾乎完全相同,您還可以在用戶選擇“取消”按鈕后重新加載表格。

正如我所說,它非常簡單,可能不符合您的期望,但是在用戶單擊“取消”后它會取消您的左滑。 我希望這個對你有用。

我遵循了這種方法並且效果很好

private func onDeleteNote(noteIdx: Int) {
        let noteToDelete: Note = myNotes[noteIdx]
        
        let alertViewController = UIAlertController(title: "¿Would you like to delete this note?", message: "The Note \(noteToDelete.title) will be delete", preferredStyle: .alert)
        
        alertViewController.addAction(UIAlertAction(title: "Delete", style: .destructive) { (UIAlertAction) in
            print("Delete note")
        })
        alertViewController.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (UIAlertAction) in
            print("Delete note was cancel")
            self.myNotesTable.reloadRows(at: [IndexPath(row: noteIdx, section: 0)], with: .right)
        })
        
        present(alertViewController, animated: true, completion: nil)
        
    }

當用戶取消操作時,我嘗試重新加載此表格單元格以丟棄滑動狀態。

暫無
暫無

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

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