ios - 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 上滑动时没有任何反应,任何人都可以看到任何原因吗?
谢谢
解决方案
如文档所述,您需要为单元设置委托:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! SwipeTableViewCell
cell.delegate = self
return cell
}
推荐阅读
- html - json管道不显示正则表达式数据
- networking - 我将如何解析 rust 中的多部分表单数据?
- r - 如何使用 R 根据另一个数据框中的 colnames 分类对数据框的多个列进行重新分类和合并?
- javascript - Vue 3 在标签中保留空类属性
- vue.js - Vue CLI:为什么 createApp 可以工作,而 Vue.createApp 不能?
- python - 在键列的子值上连接两个数据框
- sql - 带有函数返回 VARCHAR2 的 WITH 子句
- encoding - 覆盆子 PICO 的 cmake 文件编码问题
- excel - 需要带有动态字段的sumproduct excel公式
- javascript - 比较两个句子并识别javascript中的错误单词