SwipeCellKit无法正常工作

时间:2017-12-31 08:00:46

标签: ios swift

我不知道为什么我现在无法从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]



    }
}

这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

如果您查看示例应用程序,您会发现需要调用.acction.fullfill(带:.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)")
            }

}