ios - SwipeCellKit 无法正常工作

标签 ios swift

我不知道为什么我现在不能从 stackoverflow 上传图片,所以如果我使用外部链接来显示我的问题,我很抱歉

我正在尝试在我的简单待办事项列表应用程序中实现 SwipeCellKit。我希望当我滑动我的表格 View 单元格时,我会产生像这样的巨大破坏效果

https://raw.githubusercontent.com/jerkoch/SwipeCellKit/develop/Screenshots/Expansion-Destructive.gif

但是当我实现代码时,它没有破坏性的滑动效果,甚至不能像这样滑动删除

https://ibb.co/mg7w4w

这里是SwipeCellKit相关的代码实现

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "toDoItemCell", for: indexPath) as! SwipeTableViewCell

        cell.delegate = self // for swipe cell

        if let item = toDoItems?[indexPath.row] {
            cell.textLabel?.text = item.title
            cell.accessoryType = (item.isItDone) ? .checkmark : .none

            // to darken the cell color,
            let colorFromCategory = UIColor(hexString: (selectedCategory?.color)!)

            if let color = colorFromCategory?.darken(byPercentage: CGFloat(indexPath.row)/CGFloat(toDoItems!.count)) {

                // if there is a member in the toDoList Array,  'count' is not nil, then set the background and text color
                cell.backgroundColor = color
                cell.textLabel?.textColor = ContrastColorOf(color, returnFlat: false)
            }



        } else {
            cell.textLabel?.text = "You have no items yet"
        }


        return cell
    }

// MARK: - Swipe Table view Cell Delegate

extension ToDoListVC : 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
            // delete selected category from Realm Database

            // if Categories is not nil then delete the realm database

            if let deletedItem = self.toDoItems?[indexPath.row] {

                do {
                    try self.realm.write {
                        self.realm.delete(deletedItem)
                    }
                } catch {
                    print("error while deleting item from Realm Database: \(error)")
                }

                tableView.reloadData()

            }
        }


        // to edit swipe behaviour
        func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation:
            SwipeActionsOrientation) -> SwipeTableOptions {
            var options = SwipeTableOptions()
            options.expansionStyle = .destructive
            options.transitionStyle = .border
            return options
        }




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

        return [deleteAction]



    }
}

这里出了什么问题?

最佳答案

如果您查看示例应用程序,您会发现您需要调用 .action.fulfill(with: .delete):

if let deletedItem = self.toDoItems?[indexPath.row] {

            do {
                try self.realm.write {
                    self.realm.delete(deletedItem)
                    action.fulfill(with: .delete)
                }
            } catch {
                print("error while deleting item from Realm Database: \(error)")
            }

关于ios - SwipeCellKit 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48040933/

相关文章:

ios - 如何在 Ios 中使用自动布局制作垂直间距黑白 UIFields

ios - 大型 uitableview(1000 行)在 reloadData() 上卡住

ios - 具有自动布局的 UITableView 中的动态单元格高度

ios 如何读取崩溃日志

ios - PHImageManager 获取全尺寸图像

ios - UIView 动画在第一次尝试时没有动画

ios - 切换 View Controller

ios - AVPlayer 宽高比

ios - 我应该做什么来设置 UICollectionView 的约束

swift - 如何合并两个 Firebase 快照