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

我正在尝试在简单的待办事项列表应用程序中实现SwipeCellKit。我希望当我滑动表格视图单元格时,我将具有很好的解构效果

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/

10-13 05:05