【问题标题】:SwiftUI - List editing mode - how to change delete button title?SwiftUI - 列表编辑模式 - 如何更改删除按钮标题?
【发布时间】:2020-01-19 18:07:15
【问题描述】:

有没有办法在编辑列表时更改删除按钮的标题?

例子-


struct ContentView: View  {

    @State private var users = ["Paul", "Taylor", "Adele"]

    var body: some View {
        NavigationView {
            List {
                ForEach(users, id: \.self) { user in
                    Text(user)
                }.onDelete(perform: delete)
            }.navigationBarItems(trailing: EditButton())
        }
    }

    func delete(source: IndexSet) { }
}

【问题讨论】:

  • 你试过使用 UIViewControllerRepresentable / UIViewRepresentable 吗?

标签: ios swift iphone swiftui


【解决方案1】:

从 Xcode 11.3.1 开始,SwiftUI 不支持列表项的自定义滑动操作。根据 Apple SDK 发展的历史,我们可能要等到下一个主要 SDK 版本(在 WWDC 2020 上)或之后才能看到支持。

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

请注意,您必须是 beta 4 或更高版本才能在 iOS 上使用 contextMenu 修饰符。

看到这个 - SwiftUI - Custom Swipe Actions In List

【讨论】:

  • 现在可以从 XCode 12.3 开始吗?
【解决方案2】:

如果你想在 15.0 以下采用这个,试试这个。

为此,您需要Introspect

List {
        ContentsView
        .introspectTableView { tv in
            tv.delegate = viewModel
        }
}

ViewModel 应该是……

final class MyCustomViewModel: NSObject, ObservableObject, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "Kick Out!"
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多