我对swift代码相当陌生,在创建行a事件后,我在从表视图中删除行/事件时遇到了麻烦,一切正常,但当我单击删除时,我得到了一个错误,导致应用程序终止,这是下面的代码
import UIKit
var selectedDate = Date()
class WeeklyViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,
UITableViewDelegate, UITableViewDataSource
{
@IBOutlet weak var monthLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var collectionView: UICollectionView!
var event = Event()
var totalSquares = [Date]()
override func viewDidLoad()
{
super.viewDidLoad()
setCellsView()
setWeekView()
}
func setCellsView()
{
let width = (collectionView.frame.size.width - 2) / 7
let height = (collectionView.frame.size.height - 2)
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flowLayout.itemSize = CGSize(width: width, height: height)
}
func setWeekView()
{
totalSquares.removeAll()
var current = CalendarHelper().sundayForDate(date: selectedDate)
let nextSunday = CalendarHelper().addDays(date: current, days: 7)
while (current < nextSunday)
{
totalSquares.append(current)
current = CalendarHelper().addDays(date: current, days: 1)
}
monthLabel.text = CalendarHelper().monthString(date: selectedDate)
+ " " + CalendarHelper().yearString(date: selectedDate)
collectionView.reloadData()
tableView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
totalSquares.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calCell", for: indexPath) as! CalendarCell
let date = totalSquares[indexPath.item]
cell.dayOfMonth.text = String(CalendarHelper().dayOfMonth(date: date))
if(date == selectedDate)
{
cell.backgroundColor = UIColor.systemBlue
}
else
{
cell.backgroundColor = UIColor.white
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
selectedDate = totalSquares[indexPath.item]
collectionView.reloadData()
tableView.reloadData()
}
@IBAction func previousWeek(_ sender: Any)
{
selectedDate = CalendarHelper().addDays(date: selectedDate, days: -7)
setWeekView()
}
@IBAction func nextWeek(_ sender: Any)
{
selectedDate = CalendarHelper().addDays(date: selectedDate, days: 7)
setWeekView()
}
override open var shouldAutorotate: Bool
{
return false
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return Event().eventsForDate(date: selectedDate).count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! EventCell
event = Event().eventsForDate(date: selectedDate)[indexPath.row]
cell.eventLabel.text = event.name + " " + CalendarHelper().timeString(date: event.date)
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){
if editingStyle == .delete{
self.event.name.removeFirst()
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
override func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated)
tableView.reloadData()
}
}
这是我得到的错误
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函数上,因为
if editingStyle == .delete{
self.event.name.removeFirst() //This line is the problem
tableView.deleteRows(at: [indexPath], with: .fade)
1条答案
按热度按时间8cdiaqws1#
当您呼叫deleteRows(at:with:)之后,数据来源长度错误时,就会发生这个错误。
这是数据源中的行数
并且在更新表视图时,应该更新同一个数据源,但使用
它们显然不是同一个数据源
尝试使用全局属性来存储数据源,我发现您到处都在示例化Event(),这是错误的。