2017-06-01 47 views
0

我是一個初學者,在iOS開發中很快。我面臨的問題是:我正在使用CoreData構建應用程序,該應用程序包含表格視圖和表格單元格。我無法解釋,因爲我缺乏知識,所以我分享了截圖。我看過其他問題,他們都沒有解決我的錯誤。 THIS IS Portion Have ThreadTHIS IS AppDelegate Thread我也爲AppDelegate的情況下這是tabelview RowInSection致命錯誤swift

@available(iOS 10.0, *) 
let ad = UIApplication.shared.delegate as! AppDelegate 
@available(iOS 10.0, *) 
let context = ad.persistentContainer.viewContext 

我對VC的代碼是

import UIKit 
import CoreData 

class MainVC: UIViewController, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate { 

    @IBOutlet weak var tableViewmain: UITableView! 

    @IBOutlet weak var topSegment: UISegmentedControl! 

    var fetchResultControll: NSFetchedResultsController<Items>! 

    override func viewDidLoad() { 

     super.viewDidLoad() 

     tableViewmain.delegate = self 
     tableViewmain.dataSource = self 

     doFetch() 
    } 

    func configureCell (cell: ItemsCell, indexPath: IndexPath) { 

     let item = fetchResultControll.object(at: indexPath) // remember as here 
     cell.confugringCell(item: item) 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableViewmain.dequeueReusableCell(withIdentifier: "ItemsCell", for: indexPath) as! ItemsCell 
     configureCell(cell: cell, indexPath: indexPath) 

     return cell 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     if let sections = fetchResultControll.sections{ 

      let sectionInfo = sections[section] 

      return sectionInfo.numberOfObjects    
     } 
     return 0 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     if let allSections = fetchResultControll.sections { 

      return allSections.count 
     } 
     return 0 
    } 

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

    func doFetch() { 

     let fetchRequest: NSFetchRequest<Items> = Items.fetchRequest() 
     let dateSrot = NSSortDescriptor(key: "created", ascending: false) 
     fetchRequest.sortDescriptors = [dateSrot] 

     if #available(iOS 10.0, *) { 
      let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)  

      do { 
       try controller.performFetch() 
      } 
      catch {      
       let err = error as NSError 
       print("\(err)") 
      } 

     } else { 
      // Fallback on earlier versions 
     } 
    } 
    //controler willchnge 
    func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { 
     tableViewmain.beginUpdates() 
    } 
    //controlerdidchnge 
    func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { 
     tableViewmain.endUpdates() 
    } 
    //controlerdidchangeanobject 
    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { 

     switch(type) { 

     case .insert: 
      if let indexpath = newIndexPath { 
       tableViewmain.insertRows(at: [indexpath], with: .fade) 
      } 
      break 
     case .delete: 
      if let indexpath = indexPath { 
      tableViewmain.deleteRows(at: [indexpath], with: .fade) 
      } 
      break 
     case .update: 
      if let indexpath = indexPath { 
      let cell = tableViewmain.cellForRow(at: indexpath) as! ItemsCell 
      configureCell(cell: cell, indexPath: indexpath) // as used here 
      } 
      break 
     case .move: 
      if let indexpath = indexPath { 
      tableViewmain.deleteRows(at: [indexpath], with: .fade) 

      } 
      if let indexpath = newIndexPath { 
       tableViewmain.insertRows(at: [indexpath], with: .fade) 
      } 
      break 
     }    
    } 

我希望你明白我..任何幫助將高度讚賞

+1

fetchedResultsController初始化在哪裏?根據你的代碼它是'nil'並導致錯誤。 – vadian

+0

顯示確切的行,你得到的錯誤 –

回答

1
做了功能

將下列行替換爲您的。

var fetchResultControll: NSFetchedResultsController<Items>? 
+0

令人難以置信的它的工作感謝你很多,我不知道如何以及爲什麼它的工作,你可以請讓我採取什麼是解決它.. – AsnChalnger