I been researching how to delete a row from a table view which the data is in Core Data. I can't find anything online that would help me with my code. Im really new with Core Data so I don't really understand much of it.
Thanks in advance
This is my Code
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return}
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "OfficialPT")
fetchRequest.sortDescriptors = [NSSortDescriptor(key:"createdAt",ascending:false)]
do {
AirForceDatabase = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
func SideMenus ()
{
if revealViewController() != nil {
MenuButton.target = revealViewController()
MenuButton.action = #selector(SWRevealViewController.revealToggle(_:))
revealViewController().rearViewRevealWidth = 275
revealViewController().rightViewRevealWidth = 160
// profileButton.target = revealViewController()
// profileButton.action = #selector(SWRevealViewController.rightRevealToggle(_:))
view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
}
extension HistoryPTViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return AirForceDatabase.count
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) as? HistoryPTTableViewCell {
let person = AirForceDatabase[indexPath.row]
cell.OfficialAbsLbl!.text = person.value(forKeyPath: "officialAbs")! as? String
cell.OfficialPushLbl!.text = person.value(forKeyPath: "officialPush")! as? String
cell.OfficialSitLbl!.text = person.value(forKeyPath: "officialSit")! as? String
cell.OfficialRunLbl!.text = person.value(forKeyPath: "officialRun")! as? String
cell.officialTotal!.text = person.value(forKeyPath: "totalScore")! as? String
cell.officialdateCreated.text = person.value(forKeyPath: "createdAt")! as? String
return cell
}
return UITableViewCell()
}
} extension HistoryPTViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let person = AirForceDatabase[indexPath.row]
//Convert String to Double
if let score = person.value(forKeyPath: "totalScore")! as? String, let doubleScore = Double(score) {
officialTotalScore = doubleScore
//officialTotalScore = Double(score)
print("Score \(officialTotalScore)")
}
var createdat :String!
CompostiveLbl.text = "Composite Score:\n\(officialTotalScore)%"
createdat = person.value(forKeyPath: "createdAt")! as? String
print(createdat + "CREATED")
if officialTotalScore >= 90 {
print("Excellent")
officialFinalScoreLbl.text = "Fitness Level:\n Excellent"
}else if officialTotalScore >= 75 && officialTotalScore <= 89.9 {
print("Composite Score: Satisfactory")
officialFinalScoreLbl.text = "Fitness Level:\n Satisfactory"
}else if officialTotalScore <= 75 {
print("Composite Score: Unsatisfactory")
officialFinalScoreLbl.text = "Fitness Level:\n Unsatisfactory"
}
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.long
dateFormatter.dateFormat = "yyyy-MM-dd"
let convertedDate = dateFormatter.date(from: createdat)
dateFormatter.dateFormat = "MMM dd yyyy"
let date = dateFormatter.string(from: convertedDate!)
NextPTLbl.text = "Test Date:\n" + date
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let person = AirForceDatabase[indexPath.row]
if editingStyle == .delete {
}
}