在具有节和行的TableView中出现SwiftUI.ontap手势问题

在具有节和行的TableView中出现SwiftUI.ontap手势问题,swift,swiftui,Swift,Swiftui,我有两种型号: struct Category: Identifiable { var id = UUID() var title: String var number: Int var items: [ChecklistItem] } 以及: 与: 我尝试实现点击行来检查和取消检查tableView中带有节和行的cheklist项,但我不知道如何发布它。我的代码: struct ChecklistView: View { @Environmen

我有两种型号:

struct Category: Identifiable {
    
    var id = UUID()
    var title: String
    var number: Int
    var items: [ChecklistItem]
}
以及:

与:

我尝试实现点击行来检查和取消检查tableView中带有节和行的cheklist项,但我不知道如何发布它。我的代码:

struct ChecklistView: View {

  @EnvironmentObject var checklist: Checklist
  @State var newChecklistItemViewIsVisible = false

  var body: some View {
    NavigationView {
      List {
        ForEach(checklist.items) { category in
            Section(header: Text(category.title)) {
                ForEach(category.items) { item in
                    HStack {
                      Text(item.name)
                      Spacer()
                      Text(item.isChecked ? "✅" : "The reason you are getting that error is because structs are immutable. You should use method marked with "mutating" inside desired struct. Something like

if let matchingIndex = category.items.firstIndex(where: { $0.id == item.id }) {
    category.items[matchingIndex].toggleItem()
}
struct ChecklistView:View{
@环境对象变量检查表:检查表
@状态变量newChecklistItemViewIsVisible=false
var body:一些观点{
导航视图{
名单{
ForEach(检查表项目){类别在
章节(标题:文本(类别.标题)){
ForEach(category.items){item in
HStack{
文本(项目名称)
垫片()

文本(item.isChecked?)✅" : " 出现该错误的原因是结构是不可变的

如果let matchingIndex=category.items.firstIndex(其中:{$0.id==item.id}){
category.items[matchingIndex].toggleItem()
}
在您的结构中:

mutating func toggleItem(){
self.isChecked.toggle()
}

但我建议您改用@State,因为您试图做的事情与您表示视图的方式直接相关。稍后,当用户愿意使用该选项时,您会将该数据发送到您的模型中。出现该错误的原因是结构是不可变的。您应该使用方法ma在所需的结构中使用“mutating”标记。类似

如果let matchingIndex=category.items.firstIndex(其中:{$0.id==item.id}){
category.items[matchingIndex].toggleItem()
}
在您的结构中:

mutating func toggleItem(){
self.isChecked.toggle()
}
但我建议您改用@State,因为您试图做的事情与您表示视图的方式直接相关。稍后,当用户愿意使用该选项时,您会将数据发送到您的模型

导入SwiftUI
//对类的更改和添加NSObject结构是不可变的
类别:NSObject,可识别{
设id=UUID()
变量标题:字符串
变量编号:Int
变量项:[ChecklistItem]
//现在你需要一个init
初始化(标题:字符串,编号:Int,项:[ChecklistItem]){
self.title=标题
self.number=number
self.items=项目
}
}
//对类的更改和添加NSObject结构是不可变的
类ChecklistItem:可识别{
设id=UUID()
变量名称:String
已检查变量:Bool=false
//现在你需要一个init
init(名称:String){
self.name=名称
}
}
类检查表:ObservieObject{
@已发布的变量项=[类别]()
}
结构ChecklistView:视图{
//如果@ObservedObject来自更高的视图,则可以是@EnvironmentObject
@观察对象变量检查表:检查表=检查表()
@状态变量newChecklistItemViewIsVisible=false
var body:一些观点{
导航视图{
名单{
ForEach(检查表项目){类别在
章节(标题:文本(类别.标题)){
ForEach(category.items){item in
按钮(操作:{
打印(项目.isChecked.description)
item.isChecked.toggle()
//如果使用诸如@FetchRequest之类的工具,或者在您以某种方式通知“checklist.items”有更改之后,触发视图刷新的工具将不再是必需的
checklist.objectWillChange.send()
}) {
HStack{
文本(项目名称)
垫片()
文本(item.isChecked?)✅“:”
导入快捷界面
//对类的更改和添加NSObject结构是不可变的
类别:NSObject,可识别{
设id=UUID()
变量标题:字符串
变量编号:Int
变量项:[ChecklistItem]
//现在你需要一个init
初始化(标题:字符串,编号:Int,项:[ChecklistItem]){
self.title=标题
self.number=number
self.items=项目
}
}
//对类的更改和添加NSObject结构是不可变的
类ChecklistItem:可识别{
设id=UUID()
变量名称:String
已检查变量:Bool=false
//现在你需要一个init
init(名称:String){
self.name=名称
}
}
类检查表:ObservieObject{
@已发布的变量项=[类别]()
}
结构ChecklistView:视图{
//如果@ObservedObject来自更高的视图,则可以是@EnvironmentObject
@观察对象变量检查表:检查表=检查表()
@状态变量newChecklistItemViewIsVisible=false
var body:一些观点{
导航视图{
名单{
ForEach(检查表项目){类别在
章节(标题:文本(类别.标题)){
ForEach(category.items){item in
按钮(操作:{
打印(项目.isChecked.description)
item.isChecked.toggle()
//如果使用诸如@FetchRequest之类的工具,或者在您以某种方式通知“checklist.items”有更改之后,触发视图刷新的工具将不再是必需的
checklist.objectWillChange.send()
}) {
HStack{
文本(项目名称)
垫片()

文本(item.isChecked?)✅" : "谢谢你的回答。你能建议我如何整合移动和删除方法来
   class Checklist: ObservableObject {
    
      @Published var items = [Category]()

  func deleteListItem(whichElement: IndexSet) {
    items.remove(atOffsets: whichElement)
  }

  func moveListItem(whichElement: IndexSet, destination: Int) {
    items.move(fromOffsets: whichElement, toOffset: destination)
  }


}
struct ChecklistView: View {

  @EnvironmentObject var checklist: Checklist
  @State var newChecklistItemViewIsVisible = false

  var body: some View {
    NavigationView {
      List {
        ForEach(checklist.items) { category in
            Section(header: Text(category.title)) {
                ForEach(category.items) { item in
                    HStack {
                      Text(item.name)
                      Spacer()
                      Text(item.isChecked ? "✅" : "The reason you are getting that error is because structs are immutable. You should use method marked with "mutating" inside desired struct. Something like

if let matchingIndex = category.items.firstIndex(where: { $0.id == item.id }) {
    category.items[matchingIndex].toggleItem()
}