SwiftUI-列表中的自定义滑动动作

Max*_*Max 5 list swipe swift swiftui

如何在SwiftUI中使用自定义滑动动作?

我试图使用UIKit框架使它们在SwiftUI中工作。但这对我不起作用。

import SwiftUI
import UIKit



    init() {
        override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let important = importantAction(at: indexPath)
            return UISwipeActionsConfiguration(actions: [important])
        }
        func importantAction(at indexPath: IndexPath) -> UIContextualAction {
            let action = UIContextualAction(style: .normal, title: "Important") { (action, view, completion) in
                print("HI")
            }
            action.backgroundColor = UIColor(hue: 0.0861, saturation: 0.76, brightness: 0.94, alpha: 1.0) /* #f19938 */
            action.image = UIImage(named: "pencil")
            return action
        }
    }






struct TestView: View {

      NavigationView {
               List {
                    ForEach(appointmentsViewModel.appointments.identified(by: \.id)) { appointment in Row_Appointments(appointment: appointment)
                }.onDelete(perform: delete)
            }
        }
    }
}

Run Code Online (Sandbox Code Playgroud)

rob*_*off 20

更新

据我所知,从 Xcode 12 beta 1(在 WWDC 2020 发布)开始,情况并没有改善。

原来的

从 Xcode 11.3.1 开始,SwiftUI 不支持List项目的自定义滑动操作。根据 Apple 的 SDK 演变历史,在下一个主要 SDK 版本(在 WWDC 2020)或更高版本之前,我们不太可能看到支持。

实现不同的用户界面可能会更好,例如添加切换按钮作为列表项的子视图,或列表项添加上下文菜单


paw*_*222 9

iOS 15+

在 iOS 15 中我们终于可以使用原生的Swipe Actions了:

func swipeActions<T>(edge: HorizontalEdge = .trailing, allowsFullSwipe: Bool = true, content: () -> T) -> some View where T : View
Run Code Online (Sandbox Code Playgroud)

ForEach它们可以像onMove或 一样附加到容器上onDelete

List {
    ForEach(appointmentsViewModel.appointments.identified(by: \.id)) { appointment in
        Row_Appointments(appointment: appointment)
    }
    .swipeActions(edge: .trailing) {
        Button {
            print("Hi")
        } label: {
            Label("Important", systemImage: "pencil")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Iva*_*Rep 5

基于米夏?Ziobro使用Introspect回答以简化表视图委托设置。

请注意,这将覆盖表视图委托,并且可能会破坏一些现有的表视图行为。虽然可以通过将方法添加到自定义委托来修复诸如 header hight 之类的问题,但其他可能无法修复。

struct ListSwipeActions: ViewModifier {

    @ObservedObject var coordinator = Coordinator()

    func body(content: Content) -> some View {

        return content
            .introspectTableView { tableView in
                tableView.delegate = self.coordinator
            }
    }

    class Coordinator: NSObject, ObservableObject, UITableViewDelegate {

        func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
            return .delete
        }

        func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

            let archiveAction = UIContextualAction(style: .normal, title: "Title") { action, view, completionHandler in
                // update data source
                completionHandler(true)
            }
            archiveAction.image = UIImage(systemName: "archivebox")!
            archiveAction.backgroundColor = .systemYellow

            let configuration = UISwipeActionsConfiguration(actions: [archiveAction])

            return configuration
        }
    }
}

extension List {
    func swipeActions() -> some View {
        return self.modifier(ListSwipeActions())
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 虽然这适用于滑动操作,但它本质上破坏了与 SwiftUI 表格其余部分的任何交互。显然,被重写的委托处理了所有这些,但是将方法重新委托给原始方法并没有帮助。 (2认同)