【发布时间】:2019-12-26 08:49:07
【问题描述】:
(仅在 macOS 上尝试过,但我相信在 iOS 上会有相同的行为)
macOS 10.15.2 (19C57); Xcode:11.3 (11C29)
(问题已更新,因为我在问题中发现了新的关系)
我有以下代码:
List(model.filteredStatus) { status in
StatusViewRow(status: status, model: self.model)
.contextMenu{
Text("menu 1")
Text("menu 2")
}
}
效果很好。
但如果我想使用 ForEach 代替(不管为什么):
ForEach(model.filteredStatus) { status in
StatusViewRow(status: status, model: self.model)
.contextMenu{
Text("menu 1")
Text("menu 2")
}
}
上下文菜单不出现。
我找到了原因。原因是ForEach + contextMenu + if 语句组合不正确!
ContextMenu 不能完全在 if 语句内的部分行上工作。在 List 的 ful 行上运行良好,并且在我使用 ForEach 时仅在工具行部分上运行。
有人可以解释为什么会这样吗?
我在StatusViewRow 中有一个if 语句,contextMenu 没有出现在这部分:
struct StatusViewRow : View {
@ObservedObject var status : FileStatusViewModel
@ObservedObject var model : StatusViewModel
var body: some View {
HStack {
TaoToggle(state: status.stageState) { self.status.toggleStageState() }
// you can replace to just a toogle
// ISSUE START HERE
if(model.fileNamesOnly) {
StyledText(verbatim: status.fileName )
.style(.highlight(), ranges: { [$0.lowercased().range(of: model.filterStr.lowercased() )]})
}
else {
StyledText(verbatim: status.path )
.style(.foregroundColor(.gray), ranges: { [$0.range(of: status.fileDir) ] } )
.style(.bold(), ranges: { [$0.range(of: status.fileName) ] })
.style(.highlight(), ranges: { [$0.lowercased().range(of: model.filterStr.lowercased() )]})
}
// ISSUE END HERE
}
}
}
【问题讨论】:
-
适用于 iOS 13.2。适用于 macOS 10.15.2。使用 Xcode 11.2.1 测试。也许你有 Xcode 问题?
-
ForEach需要在List、VStack、HStack、ScrollView等内部 - 我想这可能是问题所在。 -
@Asperi macOS 10.15.2 (19C57); Xcode:版本 11.3 (11C29)
-
@LuLuGaGa 不起作用:imgur.com/cC53b1m + imgur.com/c288NJe 并且问题仍然重现。
-
发现新信息和更新问题;