首页 > 解决方案 > SwipeCellKit 滑动删除未被调用

问题描述

嗨,我正在使用 SwipeCellKit 并想滑动 .right 以删除 indexPath.row 处的一行。我调用了函数 editActionsForRowAt indexPath 并使用了下面的代码,但用户仍然无法滑动删除。

我有以下变量

var allLists:[ShoppingList] = []
var isSwipeRightEnabled = false
var defaultOptions = SwipeTableOptions()

我的代码是:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {

    if orientation == .left {
        guard isSwipeRightEnabled else { return nil }
    }

    let delete = SwipeAction(style: .destructive, title: nil) { action, indexPath in

        var list: ShoppingList

        if self.allLists.count > 0 {
            list = self.allLists[indexPath.row]
            self.allLists.remove(at: indexPath.row)
        } else {
            list = self.allLists[indexPath.row]
        }

        list.deleteItemInBackground(shoppingList: list)

        action.fulfill(with: .delete)
        self.tableView.reloadData()

    }
    configure(action: delete, with: .trash)
    return [delete]

}


func configure(action: SwipeAction, with descriptor: ActionDescriptor) {
    action.title = descriptor.title()
    action.image = descriptor.image()
    action.backgroundColor = descriptor.colour
}

func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeTableOptions {

    var options = SwipeTableOptions()
    options.transitionStyle = defaultOptions.transitionStyle
    options.buttonSpacing = 11
    return options

}

我有一个名为 SwipeTableViewHelpers.swift 的助手 .swift 文件,它是这样的:

import Foundation
import UIKit

enum ActionDescriptor {
case save, returnPurchase, trash

func title() -> String? {

    switch self {
    case .save: return "Save"
    case .returnPurchase: return "Return"
    case .trash: return "Delete"

    }
}

var colour: UIColor {
    switch self {
    case .save: return .lightGray
    case .returnPurchase: return .lightGray
    case .trash: return .red
    }
}

func image() -> UIImage? {

    let name: String
    switch self {
    case .save: name = "saveItem"
    case .returnPurchase: name = "ReturnFilled"
    case .trash: name = "Trash"
    }

    return UIImage(named: name)
}
}

func createSelectedBackgroundView() -> UIView {

let view = UIView()
view.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2)
return view
}

调用 deleteItemInBackground 的函数是:

func deleteItemInBackground(shoppingList: ShoppingList) {

    let ref = firebase.child(kSHOPPINGLIST).child(FUser.currentId()).child(shoppingList.id)
    ref.removeValue()

}

当用户在 tableViewCell 上滑动时没有任何反应,任何人都可以看到任何原因吗?

谢谢

标签: iosswiftxcodeuitableviewswipe

解决方案


如文档所述,您需要为单元设置委托:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! SwipeTableViewCell
    cell.delegate = self
    return cell
}

推荐阅读