请帮助我
//
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 {}
}
}
我想从tableview中删除一个完整的行,即标题,详细信息,类型等。我的实体名称是SavedData。我尝试了很少的教程,但他们并没有满足我的需求。当我们向右滑动然后删除出现但不工作,因为我不知道代码。
答案 0 :(得分:1)
这样你可以根据实体从coredata中删除数据:这是从coredata中提取用户实体数据的简单函数:希望它有所帮助 //从核心数据中删除信息
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);//WEB API 1st
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);//MVC 2nd
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
答案 1 :(得分:0)
我希望这对于使用核心数据中的索引位置删除特定行很有用
func tableView(_ tableView: UITableView, commit editingStyle:
UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let requestDel = NSFetchRequest<NSFetchRequestResult>(entityName: "Contacts")
requestDel.returnsObjectsAsFaults = false
do {
let arrUsrObj = try context.fetch(requestDel)
let objectUpdate = arrUsrObj as! [NSManagedObject]
context.delete(objectUpdate[indexPath.row]) // Deleting Object
} catch {
print("Failed")
}
// Saving the Delete operation
do {
try context.save()
print("deleted")
fetch()
} catch {
print("Failed saving")
}
}