Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

ios - Why is indexPath for section 1 of a tableview starting with a value of [1,0]

I have a tableView and it's split into 2 sections.

    func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

Each section has it's own FetchResultController (FRC). This is my CellForRoatAt function

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "personCell", for: indexPath) as! PersonTableViewCell
    switch(indexPath.section) {
    case 0:
        print("this is section 0")
        let person = positiveFetchedResultsController.object(at: indexPath)
        cell.personName.text = person.name
        //print("(person.name!) is the name")
        //print("(person.statement!.count) postive person statement count")

        if(person.statement!.count == 0){
            print("default")
            cell.statementAmount.text = "$0.00"
        }
        else{
            print("(person.name!) has (person.statement!.count) statement count")
            let amountTotal = person.value(forKeyPath: "statement.@sum.amountOwed") as? Decimal
            print("(amountTotal!) this is the total")
            cell.statementAmount.text = String(describing: amountTotal!)

        }

    case 1:

        print("this is section 1")
        print("(negativeFetchedResultsController.object(at: indexPath)) objects fetched")
        let person = negativeFetchedResultsController.object(at: indexPath)
        cell.personName.text = person.name

        print("(person.name!) is the name")
        print("(person.statement!.count) negative person statement count")

        if(person.statement!.count == 0){
            cell.statementAmount.text = "$0.00"
        }
        else{
            print("(person.name!) has (person.statement!.count) statement count")
            let amountTotal = person.value(forKeyPath: "statement.@sum.amountOwed") as? Decimal
            print("(amountTotal!) this is the total")
            cell.statementAmount.text = String(describing: amountTotal!)
        }
 default: cell.personName.text = "hello"
        }

This first section 0 works, like it should and I get the correct data back from the print calls. But section 1 throws me this error and crashes my app.

error: NSFetchedResultsController: no section at index 1 in sections list

CoreData: error: NSFetchedResultsController: no section at index 1 in sections list

When I print out the indexPath I get [0,0] for section 1 and [1,0] for section 1. I would think since I'm using 2 different FRCs for each section that the indexPath would start at [0,0] for each. Obviously I am wrong, but I don't know what to do to fix it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A FetchedResultsController is meant to be used by itself with a tableview, not with multiple FRCs to one tableview. So as soon as you try to use the FRC in section 1 it flips out - it only has one section that should be index 0 not index 1.

There's two ways around this:

1. Fake the indexPath for section 1

This approach is a bit buggy, I don't recommend it. But for section 1 you can manipulate the indexPath to be indexPath.row, 0 instead of indexPath.row, indexPath.section (which right now is row, 1) to make it act like the FRC is in it's own table view by itself.

2: use sectionNameKeyPath parameter of the FRC initializer to set different sections

This property is used to differentiate sections of your FRC to help support multiple sections. The best example would be if you had a name field you could use sectionNameKeyPath of name to have sections in your tableview by name (like the address book is set up).

It looks like you are trying to do some neg/pos thing here with your multiple FRCs, if so perhaps a BOOL that lets you know if it's negative or positive so they can be differentiated into either section. This might not look like the best approach since you're adding a new property to your core data model that isn't necessary other than to differentiate sections, but sometimes in core data it's required to get the functionality you are looking for.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...