swift - 如何在列表视图中创建 SwipeActionsConfigurationForRowAt - SwiftUI (iOS 13)?
问题描述
我想在 SwiftUI 中创建一个列表视图,当用户向左滑动时,我可以在其中显示两个用于不同操作的按钮。我知道有一个删除选项(.onDelete(perform: delete))但我想添加更多操作,正如我在上图中提到的那样。
我可以使用 Swift 在 UITalbleView 中轻松做到这一点。
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
}
但我的问题是:如何在 SwiftUI 中使用 List View 做到这一点?
解决方案
这是我输入的快速代码,但它提供了一个如何创建自定义 UITableView 解决方案的简单示例:
RoutineTableView(routines: routineDataSource.routines)
.trailingSwipeActionsConfiguration {
let editAction = UIContextualAction(
style: .normal,
title: "EDIT"
) { (action, sourceView, completionHandler) in
completionHandler(true)
}
editAction.backgroundColor = UIColor.darkGray
let deleteAction = UIContextualAction(
style: .destructive,
title: "DELETE"
) { (action, sourceView, completionHandler) in
completionHandler(true)
}
let actions = [deleteAction, editAction]
let configuration = UISwipeActionsConfiguration(actions: actions)
return configuration
}
.onCellPress {
print("hi there")
}
.navigationBarTitle("Routines")
和
private class CustomDataSource<SectionType: Hashable, ItemType: Hashable>: UITableViewDiffableDataSource<SectionType, ItemType> {
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
}
struct RoutineTableView: UIViewRepresentable {
let routines: [Routine]
private var onCellPress: (() -> Void)? = nil
private var trailingSwipeActionsConfiguration: (() -> UISwipeActionsConfiguration)? = nil
init(routines: [Routine]) {
self.routines = routines
}
func makeUIView(
context: UIViewRepresentableContext<RoutineTableView>
) -> UITableView {
let tableView = UITableView()
context.coordinator.update(withTableView: tableView)
return tableView
}
func updateUIView(_ uiView: UITableView, context: UIViewRepresentableContext<RoutineTableView>) {
context.coordinator.update(routines: routines)
}
// MARK: - Coordinator
func makeCoordinator() -> RoutineTableView.Coordinator {
return Coordinator(self)
}
class Coordinator: NSObject, UITableViewDelegate {
private enum Section {
case first
}
private let view: RoutineTableView
private var dataSource: UITableViewDiffableDataSource<Section, Routine>?
init(_ view: RoutineTableView) {
self.view = view
super.init()
}
func update(withTableView tableView: UITableView) {
tableView.register(RoutineTableViewCell.self)
tableView.delegate = self
let dataSource = CustomDataSource<Section, Routine>(tableView: tableView) { (tableView, indexPath, routine) -> UITableViewCell? in
let cell: RoutineTableViewCell = tableView.dequeueReusableCell(for: indexPath)
cell.configure(withRoutine: routine)
return cell
}
self.dataSource = dataSource
}
func update(routines: [Routine]) {
var snapshot = NSDiffableDataSourceSnapshot<Section, Routine>()
snapshot.appendSections([.first])
snapshot.appendItems(routines)
dataSource?.apply(snapshot, animatingDifferences: true)
}
// MARK: - <UITableViewDelegate>
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
view.onCellPress?()
}
func tableView(
_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath
) -> UISwipeActionsConfiguration? {
return view.trailingSwipeActionsConfiguration?()
}
}
}
extension RoutineTableView {
func onCellPress(
_ onCellPress: @escaping () -> Void
) -> RoutineTableView {
var view = self
view.onCellPress = onCellPress
return view
}
func trailingSwipeActionsConfiguration(
_ trailingSwipeActionsConfiguration: @escaping () -> UISwipeActionsConfiguration
) -> RoutineTableView {
var view = self
view.trailingSwipeActionsConfiguration = trailingSwipeActionsConfiguration
return view
}
}
推荐阅读
- c++ - 我无法让我的向量显示其内容 C++
- java - Apache Beam 的 BigQueryIO (Java):无法将 TIMESTAMP 字段写入 BigQuery——fastxml.jackson 异常“类型不支持”
- python - 过滤 pyspark DataFrame 时出错
- twilio - 多
在出站呼叫时,响应显示“Digits=timeout”,呼叫提前断开 - arrays - 如何通过 str 遍历并将每个标记分配给作为全局变量的数组
- flutter - 我有清单
我需要转换 ApplicationMeta 类型 - php - 找不到类“App\Http\Controllers\User”
- python - 绘制 Pytorch 回归问题的预测结果和实际结果
- r - 使用 RSelenium 在 Windows 和 Mac 上进行网络抓取
- jaeger - 如何在 Jaeger 中按标签过滤