簡體   English   中英

使用核心數據刪除可滑動滑動的TableView單元格

[英]Deleting swipe-able TableView Cells using Core Data

因此,我可以添加任務並將這些任務保留在我的應用程序中。 現在,我想做的是能夠刪除任務並保留我刪除的內容。 我正在使用可滑動滑動單元可可豆莢,以便允許用戶向左滑動並刪除項目。 但是,下面的代碼似乎並沒有達到我想要的功能。 首先,滑動手勢不起作用,其次,如果我有“ task1”,“ task2”,“ task3”,則無法刪除任務3,但可以刪除任務1,然后可以刪除任務3。此外,有時我刪除的項目仍然存在,有時卻沒有。 我的代碼在下面,非常感謝您的幫助。

class DailyTasksTableViewController: UITableViewController {

//Variables
var tasksArray = [Task]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
.....

extension DailyTasksTableViewController: SwipeTableViewCellDelegate {
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
        guard orientation == .right else { return nil }

    let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
        self.context.delete(self.tasksArray[indexPath.row])
        self.tasksArray.remove(at: indexPath.row)

        do {
            try self.context.save() 
        }catch{
            print("error saving delete \(error)")
        }
    }

    // customize the action appearance
    deleteAction.image = UIImage(named: "delete-icon")

    return [deleteAction]
}

func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
    var options = SwipeOptions()
    options.expansionStyle = .destructive
    return options
}

}

答案為“首先,滑動手勢不起作用”

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

暫無
暫無

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

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