单个表视图通过两个不同的NSFetchedResultsControllers与部分

时间:2017-05-23 09:37:43

标签: ios uitableview core-data swift3 nsfetchedresultscontroller

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

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 个答案:

答案 0 :(得分:1)

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

1)如果你希望你的数据填充表中的两个部分,那么表必须有两个部分 - 目前,你只是返回1,所以使用它(尽管你可能想要/需要根据检索的数据进行不同的处理) :

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
}

这应该可以帮助你。