SwipeCellKit 滑动删除未被调用 [英] SwipeCellKit Swipe to delete not being called

查看:25
本文介绍了SwipeCellKit 滑动删除未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用 SwipeCellKit 并希望向右滑动以删除 indexPath.row 中的一行.我已经调用了函数editActionsForRowAt indexPath并使用了下面的代码,但用户仍然无法滑动删除.

Hi I am using SwipeCellKit and would like to swipe .right to delete a line at indexPath.row. I have called the function editActionsForRowAt indexPath and have used the code below, but the user is still unable to swipe to delete.

我有以下变量

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 文件,它是这样的:

I have a helper .swift file called SwipeTableViewHelpers.swift which is this:

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的函数是:

and the function called to deleteItemInBackground is:

func deleteItemInBackground(shoppingList: ShoppingList) {

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

}

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

When the user swipes on a tableViewCell nothing happens, can anyone see any reason for this?

谢谢

推荐答案

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

As documented, you need to set the delegate for the cell:

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

这篇关于SwipeCellKit 滑动删除未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆