PLease help me out
Here is my code
//
import UIKit
import CoreData
class SavedPageViewController: UIViewController, UITableViewDataSource, UITableViewDelegate , NSFetchedResultsControllerDelegate {
@IBOutlet weak var tableView: UITableView!
var tableTitleArray = [String]()
var tableDetailArray = [String]()
var tableTypeArray = [String]()
var tableHoursArray = [String]()
var tableImageArray = [NSData]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
// core data - show
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext
var request = NSFetchRequest(entityName: "SavedIdea")
request.returnsObjectsAsFaults = false;
do {
var result:NSArray = try context.executeFetchRequest(request)
if (result.count > 0) {
self.tableTitleArray = result.valueForKey("heading") as! [String]
self.tableDetailArray = result.valueForKey("detail") as! [String]
self.tableTypeArray = result.valueForKey("type") as! [String]
self.tableHoursArray = result.valueForKey("time") as! [String]
self.tableImageArray = result.valueForKey("image") as! [NSData]
}else{
print("0 result appear....error")}
tableView.reloadData()
}catch {
let fetchError = ("returned error is \(error as! NSError)")
print(fetchError)
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableTitleArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as! OTPTableViewCell
cell.LabelTitle.text = tableTitleArray[indexPath.row]
cell.LabelDetail.text = tableDetailArray[indexPath.row]
cell.LabelType.text = tableTypeArray[indexPath.row]
cell.LabelHours.text = String(tableHoursArray[indexPath.row])+"h"
if tableImageArray[indexPath.row].length > 0 {
cell.ImgView.image = UIImage(data: tableImageArray[indexPath.row])
}else{
cell.imageWidthConstraints.constant = 0
}
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext
/* let deleterequest = NSFetchRequest(entityName: "SavedIdea")
deleterequest.returnsObjectsAsFaults = false
do {
let savedData = try context.executeFetchRequest(deleterequest)
if savedData.count > 0 {
for result: AnyObject in savedData{
context.deleteObject(result.valueForKey("heading"))
print("NSManagedObject has been Deleted")
}
try context.save() } } catch {}
}
}
i want to delete a complete row from tableview i.e heading,detail,type etc . My entity name is SavedData. I tried few tutorial but they didnt satisfy my needs . when we slide right then delete appear but dont working as i dont know the code.
NSManagedObjectbecause you threw away the reference, then you have to remove the item at row index in all arrays representing the data source, then you have to delete theNSManagedObjectin Core Data and finally calldeleteRowsAtIndexPathsin the tableView.