.ondelete 带有部分的 SwiftUI 列表

2024-01-26

我无法发布在 SwiftUI 列表中删除和移动行的方法。

我有类别模型:

struct Category: Identifiable {

    var id = UUID()
    var title: String
    var number: Int
    var items: [ChecklistItem]

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

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


}

检查清单项目:

struct ChecklistItem: Identifiable {
  
  let id = UUID()
  var name: String
  var isChecked = false
    
}

和清单:

class Checklist: ObservableObject {

  @Published var items = [Category]()
}

这是我的列表视图:

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 ? "✅" : "????")
                    }
                    .background(Color.white)
                    .onTapGesture {
                        if let matchingIndex =
                            checklist.items[category.number].items.firstIndex(where: { $0.id == item.id }) {
                            checklist.items[category.number].items[matchingIndex].isChecked.toggle()
                      }
                    }
                }
                .onDelete(perform: checklist.items[category.number].deleteListItem)
                .onMove(perform: checklist.items[category.number].moveListItem)
            }
        }
      }
      .navigationBarItems(
        leading: Button(action: {
            self.newChecklistItemViewIsVisible = true

        }) {
          HStack {
            Image(systemName: "plus.circle.fill")
            Text("Add")
          }
        },
        trailing: EditButton()
      )
      .navigationBarTitle("List")
    }
    .onAppear {
        //print("ContentView appeared!")
    }
    .sheet(isPresented: $newChecklistItemViewIsVisible) {
      NewChecklistItemView(checklist: self.checklist)
    }
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
      ChecklistView()

  }
}

我无法使用方法 .ondelete 和 .onmove,因为我无法在结构中使用变异方法。如何更改代码以添加删除和移动带有部分的列表中的项目的功能?


你需要使用mutating函数的修饰符和更新的代码

struct Category: Identifiable {

    // ... other code

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

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

和用法就像

.onDelete { indexSet in 
   checklist.items[category.number].deleteListItem(indexSet) 
}
.onMove { indexSet, dest in 
   checklist.items[category.number].moveListItem(indexSet, dest) 
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

.ondelete 带有部分的 SwiftUI 列表 的相关文章

  • 记录使用 OpenAL 播放的样本

    我在 iOS 上使用 OpenAL 同时播放 9 个循环 为了使循环 100 同步 它们开始在不同的线程上运行 有关使用 OpenAL 记录正在播放的内容的任何指示 教程 如果我使用不同的线程 我会遇到录制问题吗 iOS 上的 OpenAL
  • 是否可以?相机 API ios [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我想在应用程序中实现一项功能 当用户
  • 使 Swift 类符合需要 init 的协议

    我有以下内容protocol in Swift protocol FooConvertible typealias FooType init foo FooType 我会做Swift类在类定义中符合它 class Bar FooConver
  • 使用 Swift 更改整个应用程序中的 UILabel 文本颜色

    在 Swift 中有什么方法可以在整个应用程序中立即更改 UILabel 的文本颜色属性吗 我尝试过使用外观属性 但这不适用于 UILabel textColor 任何方式或任何同样工作的库 一种方法是使用颜色设置 首先在您的 xcasse
  • 如何避免父ScrollView裁剪内部ScrollView?

    When a horizontal child ScrollView is nested inside a vertical parent ScrollView internal element is clipped by parent S
  • 可达性更改通知应仅调用一次

    我在我的 swift 项目中使用了 Reachability 我在 AppDelegate 中有以下代码 NSNotificationCenter defaultCenter addObserver self selector reacha
  • 即席分发失败

    我在一家大公司工作 正在开发一个适用于 iOS 5 的 iOS 应用程序 分发应用程序的唯一方式是通过临时部署 我拥有自己的服务器已经有一段时间了 由 o2switch 法国托管商 托管 当我开始开发时 我们使用它来部署应用程序以进行 Be
  • 相机叠加图片

    edit 3 好消息和坏消息 好消息是 在连接检查器中 通过断开覆盖 UIToolbar 并连接 UIImageview 我看到theKing 但是 坏消息 我没有看到我也需要的 UIToolbar 所以现在的问题是 当用户完成这里操作后
  • 取消交互式 UINavigationController 弹出手势不会调用 UINavigationControllerDelegate 方法

    如果拖动 a 的边缘UIViewController开始交互式流行过渡UINavigationController the UIViewController在电流下方有viewWillAppear 调用 然后是UINavigationCon
  • 如何将 #ifdef DEBUG 添加到 Xcode?

    我的项目中有一些代码永远不应该在发布版本中使用 但在测试时很有用 我想做这样的事情 ifdef DEBUG Run my debugging only code endif 在 Xcode 4 中哪里添加 DEBUG 设置 我尝试将其放入
  • 将多个数组合并为一个数组

    如何将多个数组合并为一个二维数组 鉴于我有以下输入 var arr1 1 2 3 var arr2 a b c var arr3 aa bb cc 我需要这样的输出 1 a aa 2 b bb 1 c cc 我认为你想要的是将三个数组组合成
  • iPhone / iPad IOS 应用程序仪器内存计数与 task_info 内存计数

    我一直在使用 Instruments Leak Tester 它给出了大约 1 3 meg 的应用程序总分配数字 但是 当使用 task info 时 它会报告更大的内存量 例如 10 20 meg 我想我只是想确认task info正在返
  • 获取 Swift 子目录中资源的所有 URL

    我正在尝试为 iOS 应用程序的子目录中的所有资源创建 URL 数组 我似乎无法到达正确的路径 即使我不知道名称 我也希望能够检索 URL 即我不想将文件名硬编码到代码中 Below is a screen shot of the hier
  • 配置 2 在按钮 swiftUI 中发出警报消息

    我要学习 swift 和 swiftUI 我申请按类别整理笔记 如果需要的话 你可以在我的 GitHub 中找到我的项目 https github com yoan8306 List Notes https github com yoan8
  • 在应用程序内启用或禁用 Iphone 推送通知

    我有一个 iPhone 应用程序 可以接收推送通知 目前 我可以通过转到 iPhone 设置 通知来禁用我的应用程序的推送通知 但我想在我的应用程序中添加一个开关或按钮来启用或禁用推送通知 这是可以做到的 因为我在 foursquare i
  • extern NSString *const 在类中。

    你好 我有这个头文件 import
  • 使用 NSOutlineView 作为文件系统目录浏览器的 Swift 代码

    我已经在这段 Swift 代码上苦苦挣扎了一段时间 但没有发现问题 代码 下面应该提供文件目录作为 NSOutlineView 的数据源 GUI 非常简单 只是一个带有 NSOutlineView 和 OutlineViewControll
  • Xcode 11 PDF 图像资源“保留矢量数据”在 SwiftUI 中不起作用?

    我正在尝试在 Xcode 11 中使用 SwiftUI 的应用程序中使用 Single Scale 来使用基于矢量的 PDF 图像 但当我放大图像尺寸时 图像总是看起来模糊 我在 Xcode 11 的 UIKit 中没有遇到任何问题 我创建
  • Swift 3 和 Xcode8 - init 的使用不明确

    在我安装 Xcode 8 并将项目转换为 Swift 3 之前 以下行没问题 现在转换后看起来像这样 let valueData Data Data bytes UnsafePointer
  • 如何将 NSAppTransportSecurity 添加到 Cordova 项目

    我正在从事一个 ionic cordova 项目 该应用程序需要配置 iOS 9 版本的应用程序传输安全例外 有谁知道如何将以下配置添加到 cordova 项目配置文件中 配置 xml

随机推荐