我有一个这样的数据模型。数据连接到核心数据
import Foundation
import CoreData
@objc(Aktie) class Aktie: NSManagedObject {
@NSManaged var isCompleted: Bool
@NSManaged var counter: String?
@NSManaged var deletedDate: Date?
@NSManaged var id: NSNumber!
@NSManaged var notes: String?
@NSManaged var title: String?
@NSManaged var type: String?
@NSManaged var point: String?
@NSManaged var saving: String?
}
我已经在我的tableview中做了一个切换-相关代码如下:
var akties: [Aktie]=[]
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {
let aktieCell=tableView.dequeueReusableCell(withIdentifier: "aktieCellID", for: indexPath) as! AktieCell
let thisAktie: Aktie!
thisAktie=nonDeletedNotes()[indexPath.row]
aktieCell.aktieTitle.text=thisAktie.title
aktieCell.aktieType.text=thisAktie.type
aktieCell.AktieCounter.text=thisAktie.counter
aktieCell.AktiePoints.text=thisAktie.point
aktieCell.accessoryType=thisAktie.isCompleted ? .checkmark: .none
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let complete=UIContextualAction(style: .normal, title: "Completed") {
(action, sourceView, completionHandler) in // print("Completed")
let cell=tableView.cellForRow(at: indexPath) as! AktieCell
self.akties[indexPath.row].isCompleted=(self.akties[indexPath.row].isCompleted) ? false: true
cell.accessoryType=(self.akties[indexPath.row].isCompleted) ? .checkmark: .none
completionHandler(true)
}
complete.backgroundColor=.systemGreen
complete.image=self.akties[indexPath.row].isCompleted ? UIImage(named: "undo"): UIImage(named: "tick")
let swipeConfiguration=UISwipeActionsConfiguration(actions: [complete])
return swipeConfiguration
}
代码构建应用程序没有任何错误,但当我尝试滑动行时,它停止并给我以下错误:
线程1:Fatal error: Index out of range.
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let complete = UIContextualAction(style: .normal, title: "Completed") { [self] (action, sourceView, completionHandler) in
let cell = tableView.cellForRow(at: indexPath) as! AktieCell
let selectedAktie : Aktie!
selectedAktie = self.nonDeletedNotes()[indexPath.row]
selectedAktie.isCompleted = (selectedAktie.isCompleted) ? false : true
cell.accessoryType = (selectedAktie.isCompleted) ? .checkmark : .none
completionHandler(true)
}
complete.backgroundColor = .systemGreen
let selectedAktie : Aktie!
selectedAktie = self.nonDeletedNotes()[indexPath.row]
complete.image = selectedAktie.isCompleted ? UIImage(named: "undo") : UIImage(named: "tick")
let swipeConfiguration = UISwipeActionsConfiguration(actions: [complete])
return swipeConfiguration
}