【问题标题】:SwiftUI List Item decoration affects slide to deleteSwiftUI 列表项装饰影响幻灯片删除
【发布时间】:2020-10-28 12:29:15
【问题描述】:

我有一个包含以下项目的列表:

 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))
  )

此代码生成如下元素,包括所选元素的圆角。

我的问题是这种单元格装饰会阻止滑动单元格向左删除以工作

我该如何解决?

  1. 我想要整个单元格,从左到右,可点击,而不仅仅是单词。
  2. 我想向左滑动。

【问题讨论】:

  • 我无法重现该问题,但我必须对其进行修改才能编译。代码格式不好,缺少括号,并且您没有在视图顶部显示变量是如何声明的。

标签: swift swiftui swiftui-list


【解决方案1】:

你可能在错误的地方附加了.onDelete,它应该附加到ForEach(不是在里面)

  ForEach(filteredItems, id: \.self) { item in
     Text(item.termLowerCase)
  }
  .onDelete { offsets in    // << here !!
    print("deleting"
  }

【讨论】:

  • 能否提供完整的可重现示例进行调试?
  • 您的回答在技术上是正确的,因为我发布的代码不正确。但我的问题超出了这个范围,在另一个班级。无论如何,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
  • 2022-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多