ios - collection view cells fetch data -
i new collection view. want retrieve data coredata collection view cell. know how retrieve table view cell failed when use similar way fetch collection view. here functions coredatahelper , viewcontroller class
override func prepare(for segue: uistoryboardsegue, sender: any?) { if let identifier = segue.identifier { if identifier == "displaycelldetail" { print("task view cell tapped") collectionviewcoredatahelper.retrievetasks() let indexpath = collectionview.indexpathsforselecteditems! let task = tasks[indexpath.row] let tasksettingviewcontroller = segue.destination as! viewcontroller tasksettingviewcontroller.task = task } else if identifier == "addtask" { print("+ button tapped") } } } static func retrievetasks() -> [tasks] { let fetchrequest = nsfetchrequest<tasks>(entityname: "tasks") { let results = try managedcontext.fetch(fetchrequest) return results } catch let error nserror { print("could not fetch \(error)") } return [] }
try :
override func prepare(for segue: uistoryboardsegue, sender: any?) { if let identifier = segue.identifier { if identifier == "displaycelldetail" { print("task view cell tapped") collectionviewcoredatahelper.retrievetasks() let cell = sender as? yourcellname //cell segue being performed let indexpath = self.collectionview!.indexpathforcell(cell) let task = self.tasks[indexpath.item] //downcast type of task let objtasksettingvc = segue.destination as! tasksettingviewcontroller objtasksettingvc.tasks = task } else if identifier == "addtask" { print("+ button tapped") } } }
Comments
Post a Comment