Home > Blockchain >  Core data - TableView - Toggle - Index out of range
Core data - TableView - Toggle - Index out of range

Time:05-08

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.

CodePudding user response:

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
    

}

  • Related