swift 如何在待办事项列表中创建节

2ic8powd  于 2023-11-16  发布在  Swift
关注(0)|答案(1)|浏览(131)

我在Swift中使用了diffable数据源。

  1. private var todos = [[ToDoItem]]() // my dataSource
  2. enum Section { // my sections
  3. case unfulfilled
  4. case completed
  5. }

字符串
通过点击任务上的淘汰,任务本身应该从未完成部分移动到已完成部分。
我还被告知要对两个数组使用该方法。
也就是说,我有我通常的任务,todo将到达哪里,告诉我如何确保我有第二个分支负责第二部分,或者如何解决这个问题。
我的旧代码的一个例子:

  1. func configureSnapshot() {
  2. var snapshot = Snapshot()
  3. if todos.isEmpty { return }
  4. if todos.count == 1 {
  5. if todos.first!.first!.isCompleted {
  6. snapshot.appendSections([.completed])
  7. snapshot.appendItems(todos.first!, toSection: .completed)
  8. } else {
  9. snapshot.appendSections([.unfulfilled])
  10. snapshot.appendItems(todos.first!, toSection: .unfulfilled)
  11. }
  12. } else if todos.count == 2 {
  13. snapshot.appendSections([.unfulfilled, .completed])
  14. snapshot.appendItems(todos.first!, toSection: .unfulfilled)
  15. snapshot.appendItems(todos[1], toSection: .completed)
  16. }
  17. dataSource?.apply(snapshot, animatingDifferences: true)
  18. }


的数据
我已经记不清他们尝试了多少次了,但已经足够让我用两个法阵了。

niknxzdl

niknxzdl1#

试试这个:

  1. //backing data source
  2. private var unfulfilledToDos = [ToDoItem]()
  3. private var completedToDos = [ToDoItem]()
  4. //To-do sections
  5. enum ToDoSection: Int, CaseIterable {
  6. case unfulfilled
  7. case completed
  8. }
  9. /// call this when you want tableview/collectionview to reload data
  10. func reloadData(shouldAnimate: Bool) {
  11. var snapshot = NSDiffableDataSourceSnapshot<ToDoSection, ToDoItem>()
  12. //if you have no data for that section, tableview/collectionview will create the section with 0 height - thereby rendering the section invisible
  13. //however, if you have content insets on that section, it will still create those content insets on a section that is not visible. if this doesn't work for your UX, then simply don't append that section below
  14. snapshot.appendSections(ToDoSection.allCases)
  15. ToDoSection.allCases.forEach { eachSection in
  16. switch eachSection {
  17. case .unfulfilled:
  18. snapshot.appendItems(unfulfilledToDos, toSection: eachSection)
  19. case .completed:
  20. snapshot.appendItems(completedToDos, toSection: eachSection)
  21. }
  22. }
  23. dataSource?.apply(snapshot, animatingDifferences: shouldAnimate, completion: { [weak self] in
  24. //reload of data is completed
  25. })
  26. }

字符串

展开查看全部

相关问题