SwipeCellKit滑动以删除未被调用

问题描述 投票:0回答:2

您好我正在使用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()

}

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

谢谢

ios swift xcode uitableview swipe
2个回答
0
投票

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

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

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
}}
© www.soinside.com 2019 - 2024. All rights reserved.