SwiftUI List Item修饰影响滑动删除
SwiftUI List Item decoration affects slide to delete
我有一个包含如下项目的列表:
List {
ForEach(filteredItems, id: \.self) { item in
Text(item.termLowerCase)
.font(fontItems)
.foregroundColor(.white)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.contentShape(Rectangle())
.onTapGesture {
// doSomething()
}
.onDelete { offsets in
print("deleting"
}
.listRowBackground(
Group {
if item == selectedItem {
Color("selectedColor").mask(RoundedRectangle(cornerRadius: 20))
} else {
Color.clear
}
}
.padding(EdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 5))
)
此代码生成如下所示的元素,包括所选元素的圆角。
我的问题是这个单元格装饰阻止滑动单元格向左删除工作
我该如何解决?
- 我想要整个单元格,从左到右,都可以点击,而不仅仅是文字。
- 我想向左滑动才能工作。
您可能将 .onDelete
附加到错误的位置,它应该附加到 ForEach
(不在行内)
ForEach(filteredItems, id: \.self) { item in
Text(item.termLowerCase)
}
.onDelete { offsets in // << here !!
print("deleting"
}
我有一个包含如下项目的列表:
List {
ForEach(filteredItems, id: \.self) { item in
Text(item.termLowerCase)
.font(fontItems)
.foregroundColor(.white)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.contentShape(Rectangle())
.onTapGesture {
// doSomething()
}
.onDelete { offsets in
print("deleting"
}
.listRowBackground(
Group {
if item == selectedItem {
Color("selectedColor").mask(RoundedRectangle(cornerRadius: 20))
} else {
Color.clear
}
}
.padding(EdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 5))
)
此代码生成如下所示的元素,包括所选元素的圆角。
我的问题是这个单元格装饰阻止滑动单元格向左删除工作
我该如何解决?
- 我想要整个单元格,从左到右,都可以点击,而不仅仅是文字。
- 我想向左滑动才能工作。
您可能将 .onDelete
附加到错误的位置,它应该附加到 ForEach
(不在行内)
ForEach(filteredItems, id: \.self) { item in
Text(item.termLowerCase)
}
.onDelete { offsets in // << here !!
print("deleting"
}