我已经完成了与此问题有关的所有Q&将简单数据(例如基本字符串(发送回tableView的方法不适用于核心数据实体(
我有一个来自核心数据实体的记录的表观视图。选择记录将带您进入详细信息。用户可以从那里删除记录。但是,当按下导航控制器的返回按钮并将用户带回表观视图时,记录仍然显示在表中。
我知道该对象已被删除,因为进一步返回一个屏幕,然后向前移动到桌面刷新它,并且该对象不再出现在记录列表中。
必须有一种简单的方法来获取返回按钮以刷新表观视图?但是如何?
我是Swift的新手,因此代码示例将不胜感激
谢谢
*************************************************************************************************
这是记录视图或第二视图控制器的代码
@IBAction func Delete(_ sender: UIButton) { // The delete function
let alertContoller = UIAlertController(title: "Delete?", message: "Are you sure? This can't be undone", preferredStyle: .actionSheet)// This is the alert message
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil) // These are the alert actions
let deleteAction = UIAlertAction(title: "Delete", style: .default) { (action) in //defining the delete action
func deleteRecords() -> Void { //The function to delete the record
let moc = getContext()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "DestinationsOne") // The fetch request
let result = try? moc.fetch(fetchRequest)
let resultdata = result as! [DestinationsOne] // result as entity
for object in resultdata { // Go through the fetched result
if object.destID == self.IDTitle{ // If there is a match
moc.delete(object) // delete the object
}
}
do {
try moc.save() // Save the delete action
} catch let error as NSError {
print("Could not save (error), (error.userInfo)")
} catch {
}
}
func getContext () -> NSManagedObjectContext { // The get context function
let appDelegate = UIApplication.shared.delegate as! AppDelegate // The appdelegate as a shared delegate
return appDelegate.persistentContainer.viewContext // Persistent container is where the objects are stored
}
deleteRecords() // Call the function
然后,用户命中了后面的按钮,以返回到名为"结果视图"。尽管已删除了有关记录,但仍显示在记录列表中。
这是来自TableView的代码:
class ResultsView: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self // Sets the delegate for the tableview to self
tableView.dataSource = self // Sets the datasource to self
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "DestinationsOne") // The request to retrieve the data from the database
let context = appDelagate.persistentContainer.viewContext // Persistant container allows the data to be saved by core data
do {
try destArray = context.fetch(request) as! [DestinationsOne]} // Putting the data into an array for processing
catch{
//error message
}
// Do any additional setup after loading the view.
}
var seasonArray = [DestinationsOne]() // The array to hold search results from find by season
var destArray = [DestinationsOne]() // The full data array
let appDelagate = UIApplication.shared.delegate as! AppDelegate // The appdelegate as a shared delegate
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // Function copied from UITableView Source
if seasonArray.count > 0{ // If there are elements in seasonArray
return seasonArray.count // sets the number of rows to the number of the seasonArray's destination objects
}else{ // If not
return destArray.count // sets the number of rows to the number of the destArray's destination objects
}
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ // Function copied from UITableView Source
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell // Recycle cells for scrollng
if seasonArray.count > 0{ // If there are elements in seasonArray
cell.Destination.text = seasonArray[indexPath.row].destName //Sets the text in the cell
if seasonArray[indexPath.row].destImage == nil{ // A check to make sure the image doesn't return nil and cause a crash
cell.Picture.image = UIImage(named:"Generic.png")!
}else{ // if not
cell.Picture.image = UIImage(data: seasonArray[indexPath.row].destImage! as Data) // The table image is either a default image or the image of record
}
return cell
}else{ // If seasonArray is empty
cell.Destination.text = destArray[indexPath.row].destName //Sets the text in the cell
if destArray[indexPath.row].destImage == nil{ // A check to make sure the image doesn't return nil and cause a crash
cell.Picture.image = UIImage(named:"Generic.png")!
}else{
cell.Picture.image = UIImage(data: destArray[indexPath.row].destImage! as Data) // The table image is either a default image or the image of record
}
return cell
}
}
我弄清楚了。ViewWillAppear中的ReloAddata删除了记录,但留下了一个占位符,如果单击,将会崩溃。解决方案是将ViewDidload中的代码也移至ViewWillAppear。
override func viewWillAppear(_ animated: Bool) {
tableView.delegate = self // Sets the delegate for the tableview to self
tableView.dataSource = self // Sets the datasource to self
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "DestinationsOne") // The request to retrieve the data from the database
let context = appDelagate.persistentContainer.viewContext // Persistant container allows the data to be saved by core data
do {
try destArray = context.fetch(request) as! [DestinationsOne]} // Putting the data into an array for processing
catch{
//error message
}
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}