swift 表格视图删除行快速

cu6pst1q  于 2022-10-31  发布在  Swift
关注(0)|答案(1)|浏览(182)

我对swift代码相当陌生,在创建行a事件后,我在从表视图中删除行/事件时遇到了麻烦,一切正常,但当我单击删除时,我得到了一个错误,导致应用程序终止,这是下面的代码

  1. import UIKit
  2. var selectedDate = Date()
  3. class WeeklyViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,
  4. UITableViewDelegate, UITableViewDataSource
  5. {
  6. @IBOutlet weak var monthLabel: UILabel!
  7. @IBOutlet weak var tableView: UITableView!
  8. @IBOutlet weak var collectionView: UICollectionView!
  9. var event = Event()
  10. var totalSquares = [Date]()
  11. override func viewDidLoad()
  12. {
  13. super.viewDidLoad()
  14. setCellsView()
  15. setWeekView()
  16. }
  17. func setCellsView()
  18. {
  19. let width = (collectionView.frame.size.width - 2) / 7
  20. let height = (collectionView.frame.size.height - 2)
  21. let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
  22. flowLayout.itemSize = CGSize(width: width, height: height)
  23. }
  24. func setWeekView()
  25. {
  26. totalSquares.removeAll()
  27. var current = CalendarHelper().sundayForDate(date: selectedDate)
  28. let nextSunday = CalendarHelper().addDays(date: current, days: 7)
  29. while (current < nextSunday)
  30. {
  31. totalSquares.append(current)
  32. current = CalendarHelper().addDays(date: current, days: 1)
  33. }
  34. monthLabel.text = CalendarHelper().monthString(date: selectedDate)
  35. + " " + CalendarHelper().yearString(date: selectedDate)
  36. collectionView.reloadData()
  37. tableView.reloadData()
  38. }
  39. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  40. totalSquares.count
  41. }
  42. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  43. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calCell", for: indexPath) as! CalendarCell
  44. let date = totalSquares[indexPath.item]
  45. cell.dayOfMonth.text = String(CalendarHelper().dayOfMonth(date: date))
  46. if(date == selectedDate)
  47. {
  48. cell.backgroundColor = UIColor.systemBlue
  49. }
  50. else
  51. {
  52. cell.backgroundColor = UIColor.white
  53. }
  54. return cell
  55. }
  56. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
  57. {
  58. selectedDate = totalSquares[indexPath.item]
  59. collectionView.reloadData()
  60. tableView.reloadData()
  61. }
  62. @IBAction func previousWeek(_ sender: Any)
  63. {
  64. selectedDate = CalendarHelper().addDays(date: selectedDate, days: -7)
  65. setWeekView()
  66. }
  67. @IBAction func nextWeek(_ sender: Any)
  68. {
  69. selectedDate = CalendarHelper().addDays(date: selectedDate, days: 7)
  70. setWeekView()
  71. }
  72. override open var shouldAutorotate: Bool
  73. {
  74. return false
  75. }
  76. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  77. {
  78. return Event().eventsForDate(date: selectedDate).count
  79. }
  80. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
  81. {
  82. let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! EventCell
  83. event = Event().eventsForDate(date: selectedDate)[indexPath.row]
  84. cell.eventLabel.text = event.name + " " + CalendarHelper().timeString(date: event.date)
  85. return cell
  86. }
  87. func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  88. return true
  89. }
  90. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){
  91. if editingStyle == .delete{
  92. self.event.name.removeFirst()
  93. tableView.deleteRows(at: [indexPath], with: .fade)
  94. }
  95. }
  96. override func viewDidAppear(_ animated: Bool)
  97. {
  98. super.viewDidAppear(animated)
  99. tableView.reloadData()
  100. }
  101. }

这是我得到的错误

  1. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). Table view: <UITableView:

我非常肯定错误可能出在tableview函数上,因为

  1. if editingStyle == .delete{
  2. self.event.name.removeFirst() //This line is the problem
  3. tableView.deleteRows(at: [indexPath], with: .fade)
8cdiaqws

8cdiaqws1#

当您呼叫deleteRows(at:with:)之后,数据来源长度错误时,就会发生这个错误。
这是数据源中的行数

  1. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  2. {
  3. return Event().eventsForDate(date: selectedDate).count
  4. }

并且在更新表视图时,应该更新同一个数据源,但使用

  1. self.event.name.removeFirst()

它们显然不是同一个数据源
尝试使用全局属性来存储数据源,我发现您到处都在示例化Event(),这是错误的。

相关问题