2016-10-26 24 views
1

好吧我是這個品牌的新手,並且在瀏覽很多教程和文章時都感到有點不知所措。並花了幾個小時排序通過類似的問題,沒有運氣來修理我自己的。我有一個「AddSiteVC」,允許用戶添加或刪除放入CoreData的項目,然後顯示在我的「MainVC」的TableView中。我的問題是,當我按保存或刪除並被解僱回我的MainVC onBtnClick TableView不更新,直到我離開MainVC,然後回來。我不知道我在做什麼錯,但似乎找不到解決此問題的任何事情......我不知道我的問題在哪裏,因此我將包含大部分MainVC代碼以供參考。 任何幫助將不勝感激! 謝謝!Swift,CoreData:TableView在childVC解僱時無法重新加載數據

import UIKit 
import CoreData 

class SitesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate { 

@IBOutlet weak var tableView: UITableView! 

var controller: NSFetchedResultsController<Sites>! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.delegate = self 
    tableView.dataSource = self 

    attemptFetch() 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "SitesCell", for: indexPath) as! SitesCell 
    configureCell(cell: cell, indexPath: indexPath as NSIndexPath) 
    return UITableViewCell() 
} 

func configureCell(cell: SitesCell, indexPath: NSIndexPath) { 
    let sites = controller.object(at: indexPath as IndexPath) 
    cell.configureCell(sites: sites) 
    cell.accessoryType = .detailButton 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "AddSiteViewController" { 
     if let destination = segue.destination as? AddSiteViewController { 
      if let site = sender as? Sites { 
       destination.siteToEdit = site 
      } 
     } 
    } 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if let sections = controller.sections { 
     let sectionInfo = sections[section] 
     return sectionInfo.numberOfObjects 
    } 
    return 0 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    if let sections = controller.sections { 
     return sections.count 
    } 
    return 0 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 75 
} 

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) { 
    if let objs = controller.fetchedObjects, objs.count > 0 { 
     let site = objs[indexPath.row] 
     performSegue(withIdentifier: "AddSiteViewController", sender: site) 
    } 
} 

func attemptFetch() { 
    let fetchRequest: NSFetchRequest<Sites> = Sites.fetchRequest() 
    let alphebaticalSort = NSSortDescriptor(key: "name", ascending: true) 
    fetchRequest.sortDescriptors = [alphebaticalSort] 

    let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil) 
    controller.delegate = self 
    self.controller = controller 
    do { 
     try controller.performFetch() 
    } catch { 
     let error = error as NSError 
     print("\(error)") 
    } 
} 

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { 
    tableView.beginUpdates() 
} 

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { 
    switch (type) { 
    case.insert: 
     if let indexPath = newIndexPath { 
      tableView.insertRows(at: [indexPath], with: .fade) 
     } 
     break 
    case.delete: 
     if let indexPath = indexPath { 
      tableView.deleteRows(at: [indexPath], with: .fade) 
     } 
     break 
    case.update: 
     if let indexPath = indexPath { 
      let cell = tableView.cellForRow(at: indexPath) as! SitesCell 
      configureCell(cell: cell, indexPath: indexPath as NSIndexPath) 
     } 
     break 
    case.move: 
     if let indexPath = indexPath { 
      tableView.deleteRows(at: [indexPath], with: .fade) 
     } 
     if let indexPath = newIndexPath { 
      tableView.insertRows(at: [indexPath], with: .fade) 
     } 
     break 
    } 
} 

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { 
    tableView.endUpdates() 
} 
} 
+1

放attemptFetch()() –

+0

我把attemptFetch()在viewWillAppear中(),並在viewDidLoad中刪除它()如你所說,它不似乎改變了一切。它仍然不會顯示我的新單元格,直到我離開MainVC並再次返回,但有一個例外......當我在當前按字母順序排序後添加一個以字母開頭的單元格時。示例 - 如果我在M更新後立即添加以字母開頭的任何內容,但在M之前沒有,則單元格1中的「Home」和單元格2中的「Manns」。所以也許我的NSSortDescriptor與這件事有關...?但是,這並不能解釋爲什麼刪除單元格不更新... – caleb81389

+0

在執行提取以更新表格後調用tableview 的reloadData()。 –

回答

0

請在代碼中進行以下更改。因爲在加載viewcontroller時將調用viewDidLoad。但根據您的要求,您可以在Modal頁面中添加一些內容。所以,你需要的代碼在viewWillAppear中移動viewWillAppear

import UIKit 
import CoreData 

class SitesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate { 

@IBOutlet weak var tableView: UITableView! 

var controller: NSFetchedResultsController<Sites>! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.delegate = self 
     tableView.dataSource = self 


    } 

    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 
     attemptFetch() 
    } 
} 
-1
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCell(withIdentifier: "SitesCell", for: indexPath) as! SitesCell 
configureCell(cell: cell, indexPath: indexPath as NSIndexPath) 
return cell } 
+0

你應該解釋答案。只是發佈一些代碼是沒有用的。 –

相關問題