通过两个不同的带部分的 NSFetchedResultsControllers 的单个 table 视图

Single table view through two different NSFetchedResultsControllers with sections

大家早上好。我正在使用 Swift 3.1.1、Xcode 8.3.2。我需要通过两个不同的 NSFetchedResultsController 将单个 Table 视图连接到核心数据中的两个不同 tables(实体)。我已经创建了两个 NSFetchedResultsControllers,甚至从 table 获取数据,但我遇到了如何告诉 Table 视图第一个控制器应该响应第一部分而第二个控制器应该负责第二部分的问题。 我可以给你看代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tv: UITableView!

    @IBAction func pressed(_ sender: UIButton) {
        ModelA.read { table1 in
            ModelB.read { table2 in
                if table1.isEmpty {
                    ModelA.save(recordToSave: [(a: 1, b: "a"), (a: 2, b: "b"), (a: 3, b: "c")]) {
                        ModelB.save(recordToSave: [(a: 4, b: 5.0, c: true), (a: 6, b: 7.0, c: false)]) {
                            self.tvReload()
                        }
                    }
                } else {
                    self.tvReload()
                }
            }
        }
    }

    var fetchedResultsControllerForModelA = CoreDataFunctions.fetchedResultsController
    var fetchedResultsControllerForModelB = CoreDataFunctions.fetchedResultsController

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        tvReload()
    }

    func modelOfTableA(indexPath: IndexPath) -> (forLabel1: String, forLabel2: String, forLabel3: String)? {
        if let fetchedResultsControllerForModelA = CoreDataFunctions.getNSManagedObjectForIndexPathOfTable(fetchedResultsController: fetchedResultsControllerForModelA, indexPath: indexPath) {
            if let model = ModelA.read(nsmanagedobject: fetchedResultsControllerForModelA) {
                return (forLabel1: "\(model.a)", forLabel2: model.b, forLabel3: "")
            }
        }
        return nil
    }

    func modelOfTableB(indexPath: IndexPath) -> (forLabel1: String, forLabel2: String, forLabel3: String)? {
        if let fetchedResultsControllerForModelB = CoreDataFunctions.getNSManagedObjectForIndexPathOfTable(fetchedResultsController: fetchedResultsControllerForModelB, indexPath: indexPath) {
            if let model = ModelB.read(nsmanagedobject: fetchedResultsControllerForModelB) {
                return (forLabel1: "\(model.a)", forLabel2: "\(model.b)", forLabel3: "\(model.c)")
            }
        }
        return nil
    }

    func tvReload() {
        fetchedResultsControllerForModelA = CoreDataFunctions(tableName: .a).fetchedResultsController(keyForSort: ModelA.a.rawValue, searchParameters: nil)
        fetchedResultsControllerForModelB = CoreDataFunctions(tableName: .b).fetchedResultsController(keyForSort: ModelB.a.rawValue, searchParameters: nil)

        do {
            try fetchedResultsControllerForModelA?.performFetch()
            try fetchedResultsControllerForModelB?.performFetch()

            DispatchQueue.main.async {
                self.tv.reloadData()
            }
        } catch {
            print("Error")
        }
    }

    func numberOfSectionsInTableView(_ tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let sections1 = fetchedResultsControllerForModelA?.sections {
            if let sections2 = fetchedResultsControllerForModelB?.sections {
                return sections1[section].numberOfObjects + sections2[section].numberOfObjects
            }
            return sections1[section].numberOfObjects
        }
        return 0
    }

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

        if indexPath.section == 0 {
            if let modelOfTable = modelOfTableA(indexPath: indexPath) {
                cell.l1.text = modelOfTable.forLabel1
                cell.l2.text = modelOfTable.forLabel2
                cell.l3.text = modelOfTable.forLabel3
            }
        } else {
            if let modelOfTable = modelOfTableB(indexPath: indexPath) {
                cell.l1.text = modelOfTable.forLabel1
                cell.l2.text = modelOfTable.forLabel2
                cell.l3.text = modelOfTable.forLabel3
            }
        }

        return cell
    }

}

我找不到关于这个主题的任何教程,所以我在那里提问。我不想在 Core Data 中使用来自单个实体的继承,因为在现实生活中这是不可能的。 感谢您的帮助或建议!

好的 - 我下载了您的代码,但有几个问题...

1) 如果你想让你的数据填充 table 中的两个部分,table 必须有两个部分 - 目前,你只返回 1,所以使用这个(尽管你可能want/need 根据检索到的数据进行不同的处理):

func numberOfSectionsInTableView(_ tableView: UITableView) -> Int {
    if fetchedResultsControllerForModelA == nil || fetchedResultsControllerForModelB == nil {
        return 0
    }
    return 2
}

2) 对于每个部分的行数,您的代码很接近但不太正确...

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

    if section == 0 {
        if let sections = fetchedResultsControllerForModelA?.sections {
            return sections[0].numberOfObjects
        }
    } else {
        if let sections = fetchedResultsControllerForModelB?.sections {
            return sections[0].numberOfObjects
        }
    }

    return 0

}

3) 对于实际的 cellForRowAtIndexPath 数据,您的代码再次接近,但您需要跟踪每个部分要获取的数据...

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

    if indexPath.section == 0 {
        if let modelOfTable = modelOfTableA(indexPath: indexPath) {
            cell.l1.text = modelOfTable.forLabel1
            cell.l2.text = modelOfTable.forLabel2
            cell.l3.text = modelOfTable.forLabel3
        }
    } else {
        // Each "Table data model" has only one section... since this is the 2nd table section, we need
        //  to change the section of the index path for our call to the data model
        let dataIndexPath = IndexPath(row: indexPath.row, section: 0)
        if let modelOfTable = modelOfTableB(indexPath: dataIndexPath) {
            cell.l1.text = modelOfTable.forLabel1
            cell.l2.text = modelOfTable.forLabel2
            cell.l3.text = modelOfTable.forLabel3
        }
    }

    return cell
}

这应该会让你上路。