核心数据-TableView-切换 - 索引超出范围
我有这样的数据模型。数据连接到核心数据。
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:致命错误:索引超出范围。
I have a data model like this. The data is connected to core data.
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?
}
I have made a toggle in my tableview - the relevant code is as follows.
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
}
The code builds the app without any error but when I try to swipe the row it stops and gives me the following error:
Thread 1: Fatal error: Index out of range.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
}
}