首页 > 解决方案 > 如何将滑动操作添加到列表?- 斯威夫特用户界面

问题描述

我想将滑动操作添加到我的列表中以显示一些操作。

我想要的只是在列表行上添加一些向左滑动手势的操作,例如 ios 的邮件应用程序。

我找到了这个@michał-ziobro Swipe Actions Modifier解决方案,但它不起作用:/

该代码的某些部分给了我错误,我用一些扩展修复了它们,我想这可能是它不起作用的原因。

struct TableViewConfigurator: UIViewControllerRepresentable {

    var configure: (UITableView) -> Void = { _ in }

    func makeUIViewController(context: UIViewControllerRepresentableContext<TableViewConfigurator>) -> UIViewController {

        UIViewController()
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<TableViewConfigurator>) {

        let tableViews = UIApplication.nonModalTopViewController()?.navigationController?.topViewController?.view.allSubViewsOf(type: UITableView.self) ?? [UITableView]()

        for tableView in tableViews {
            self.configure(tableView)
        }
    }
}

struct ListSwipeActions: ViewModifier {

    @ObservedObject var coordinator = Coordinator()

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

        return content
            .background(TableViewConfigurator(configure: { tableView in
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                    tableView.delegate = self.coordinator
                }
            }))
    }

    class Coordinator: NSObject, ObservableObject, UITableViewDelegate {

        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            print("Scrolling ....!!!")
        }

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

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

            let isArchived = false
            let title = isArchived ? "Unarchive" : "Archive"

            let archiveAction = UIContextualAction(style: .normal, title: title, handler: {
                (action, view, completionHandler) in

                // update data source
                completionHandler(true)
            })
            archiveAction.title = title
            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())
    }
}

这是我的列表视图:

struct NotificationsTab: View {

    @ObservedObject var viewModel = NotificationsVM()

    func delete(at offsets: IndexSet) {
        viewModel.notifications.remove(atOffsets: offsets)
    }

    var body: some View {
        NavigationView {
            Group{
                if viewModel.notifications.count > 0 {
                    List{
                        ForEach(self.viewModel.notifications, id: \.id){ notification in
                            HStack(spacing: 15){
                                WebImage(url: URL(string: notification.sender!.avatar!.userAvatarPathSmall()))
                                    .resizable()
                                    .indicator(.activity)
                                    .aspectRatio(contentMode: .fill)
                                    .animation(.easeInOut(duration: 0.5))
                                    .transition(.fade)
                                    .frame(width: 48, height: 48)
                                    .cornerRadius(24)
                                Text(notification.sender!.name!).fontWeight(.bold) + Text(notification.message!)
                            }.padding(.vertical)
                        }.onDelete(perform: delete)
                    }.swipeActions()
                }else{
                    LoadingView()
                }
            }
            .navigationBarTitle("Notifications")
        }.onAppear(){
            self.viewModel.getNotifications()
        }
    }
}

struct NotificationsTab_Previews: PreviewProvider {
    static var previews: some View {
        NotificationsTab()
    }
}

标签: iosswiftswiftui

解决方案


推荐阅读