Swift:使用工具栏按钮删除TableView中的多个单元格

时间:2019-06-27 11:04:03

标签: swift uitableview button cell uitoolbar

我目前正在使用核心数据来开发待办事项应用程序。用户可以将待办事项标记为已完成。现在,我想在工具栏上添加一个按钮,该按钮应该删除单个选项卡上每个已标记的待办事项。我已经将滑动操作配置为一次删除并标记在单元格上。我无法为这种情况提供任何代码,仅仅是因为我不知道如何实现此目的。下面提供的代码显示了我如何配置删除滑动。也许这有助于提出解决方案。

override func tableView(_ tableView: UITableView,
               trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{

    let deleteAction = UIContextualAction(style: .destructive, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in

        //Haptic Feedback
        AudioServicesPlaySystemSound(1519)

        //Animation
        let range = NSMakeRange(0, self.tableView.numberOfSections)
        let sections = NSIndexSet(indexesIn: range)
        self.tableView.reloadSections(sections as IndexSet, with: .fade)

        //Call delete function from CoreDataManager.swift
        let todo = CoreDataManager.shared.getTodoItem(index: indexPath.row)
        CoreDataManager.shared.deleteItems(item2: todo)
        tableView.reloadData()
        success(true)
    })
    deleteAction.image = UIImage(named: "trash1")
    deleteAction.backgroundColor = .red

    return UISwipeActionsConfiguration(actions: [deleteAction])
}

1 个答案:

答案 0 :(得分:0)

您应该通过检查<script> //mySelector can be any element in our current case it will be "#myId" //focusout is just the event that triggers the action, it could be others also $(mySelector).focusout(function (){ var observable = ko.dataFor(this); var context = ko.contextFor(this); // I could do observable.myProperty('Some Value'); //but it could be anything, I need to retrieve the property from mySelector //how can this be done? }); </script> 并迭代每个completed = true并调用todo来从核心数据中获取所有待办事项

CoreDataManager.shared.deleteItems(item2: todo)