ios - tableview RowInSection fatal error swift

标签 ios swift core-data

我是 swift 中 IOS 开发的初学者。我面临的问题是:我正在使用 CoreData 构建一个应用程序,该应用程序包含表格 View 和表格单元格。由于我缺乏知识,我无法真正解释,所以我正在分享屏幕截图。我看过其他问题,但都没有解决我的错误。 THIS IS Portion Have ThreadTHIS IS AppDelegate Thread我还在 AppDelegate 中为上下文创建了一个函数,它是

@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
        }            
    }

我希望你理解我.. 任何帮助将不胜感激

最佳答案

将以下行替换为您的 .

var fetchResultControll: NSFetchedResultsController<Items>?

关于ios - tableview RowInSection fatal error swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44315823/

相关文章:

ios - Swift 中的幻灯片转场

ios - 如何在 xcode 中编写从 Storyboard创建的按钮?

ios - UICollectionView:水平滚动的动态单元格宽度

swift - 具有结构体静态函数的 GCD

ios - NSManagedObjectModel mergedModelFromBundles错误

iphone - 不兼容的指针类型将 'NSSortDescriptor *__strong' 发送到 'NSArray * 类型的参数

ios - 如何在没有云功能的情况下将请求从 iOS (Swift) 发送到 Dialogflow V2 API?

swift - 由于抛出的异常,让 Swift 相信函数永远不会返回

swift - iOSDropDown : Type 'UIResponder' has no member 'NSNotification'

ios - 在 TableView 部分中排序核心数据对象