SwipeCellKit滑动删除不被调用

时间:2018-08-29 22:18:38

标签: ios swift xcode uitableview swipe

嗨,我正在使用SwipeCellKit,并想向右滑动以删除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上滑动时,什么也没有发生,有人可以看到任何原因吗?

谢谢

2 个答案:

答案 0 :(得分:0)

如所记录,您需要为单元格设置委托:

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

答案 1 :(得分:0)

如果要使用ios 11进行调试,则需要实现此方法

@available(iOS 11.0, *) func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?{ 
let deleteAction = UIContextualAction(style: .normal, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
     success(true)
 })
 deleteAction.backgroundColor = .red
 return UISwipeActionsConfiguration(actions: [deleteAction])
}

其他

 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {}

或者您可以使用以下方法进行删除

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {return true}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCellEditingStyle.delete) {
  // by deleting you can update tableview
}}