英文:
SwiftUI List on macOS: Enable Selection, double click and dragging
问题
以下是翻译好的代码部分:
struct ContentView: View {
var items = ["项目1", "项目2", "项目3"]
@State private var selection: String? = nil
var body: some View {
List(selection: self.$selection) {
ForEach(self.items, id: \.self) { item in
Text(item)
}
}
.contextMenu(forSelectionType: String.self) { items in
Button("测试") {}
} primaryAction: { items in
print(items)
}
}
}
struct ContentView: View {
var items = ["项目1", "项目2", "项目3"]
@State private var selection: String? = nil
var body: some View {
List(selection: self.$selection) {
ForEach(self.items, id: \.self) { item in
Text(item)
.draggable(item)
}
}
.contextMenu(forSelectionType: String.self) { items in
Button("测试") {}
} primaryAction: { items in
print(items)
}
}
}
希望这有帮助。
英文:
I have a simple List
with using the new contextMenu(forSelectionType: ..., primaryAction: )
to enable a double click:
struct ContentView: View {
var items = ["Item1", "Item2", "Item3"]
@State private var selection: String? = nil
var body: some View {
List(selection: self.$selection) {
ForEach(self.items, id: \.self) { item in
Text(item)
}
}
.contextMenu(forSelectionType: String.self) { items in
Button("Test") {}
} primaryAction: { items in
print(items)
}
}
}
This works well.
But I also want to provide a drag option, so that I can drag out the selected items (actually in my use case I am displaying files and I want to enable the option to drag those files to the Finder or other apps). For my example I will just use the String:
struct ContentView: View {
var items = ["Item1", "Item2", "Item3"]
@State private var selection: String? = nil
var body: some View {
List(selection: self.$selection) {
ForEach(self.items, id: \.self) { item in
Text(item)
.draggable(item)
}
}
.contextMenu(forSelectionType: String.self) { items in
Button("Test") {}
} primaryAction: { items in
print(items)
}
}
}
After now adding the draggable
-modifier, the selection of the item is not working anymore. The same is happening when using the older onDrag
-modifier.
Does anyone has an idea how to provide selection and dragging?
答案1
得分: 0
Oh,我刚刚在这里找到了解决方案:https://developer.apple.com/forums/thread/664469(我在上次搜索中没有看到这个帖子)。所以解决方案将是:
struct ContentView: View {
var items = ["Item1", "Item2", "Item3"]
@State private var selection: String? = nil
var body: some View {
List(selection: self.$selection) {
ForEach(self.items, id: \.self) { item in
Text(item)
.itemProvider {
return NSItemProvider(object: item as NSString)
}
}
}
.contextMenu(forSelectionType: String.self) { items in
Button("Test") {}
} primaryAction: { items in
print(items)
}
}
}
英文:
Oh, I just found the solution here: https://developer.apple.com/forums/thread/664469 (I didn't saw this thread in my last search). So the solution will be:
struct ContentView: View {
var items = ["Item1", "Item2", "Item3"]
@State private var selection: String? = nil
var body: some View {
List(selection: self.$selection) {
ForEach(self.items, id: \.self) { item in
Text(item)
.itemProvider {
return NSItemProvider(object: item as NSString)
}
}
}
.contextMenu(forSelectionType: String.self) { items in
Button("Test") {}
} primaryAction: { items in
print(items)
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论