为什么tableview的第1部分的indexPath以[1,0]开头 [英] Why is indexPath for section 1 of a tableview starting with a value of [1,0]

查看:107
本文介绍了为什么tableview的第1部分的indexPath以[1,0]开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableView,它分为2个部分。

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

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

每个部分都有自己的FetchResultController(FRC )。这是我的CellForRoatAt函数

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

此第一部分0正常工作,应该这样我从打印电话中获得了正确的数据。但是第1节将引发此错误并使我的应用程序崩溃。

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:错误:NSFetchedResultsController:在节列表中索引1处没有节

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

当我打印出indexPath时,我得到的第1部分是[0,0],第1部分是[1,0]。我想,因为我为每个部分使用2个不同的FRC,所以indexPath将分别从[0,0]开始。显然我错了,但是我不知道要怎么解决。

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.

推荐答案

应该使用FetchedResultsController本身具有一个表格视图,而不是一个表格视图具有多个FRC。因此,一旦您尝试在第1部分中使用FRC,它就会翻转出来-它只有一个应为索引0而不是索引1的部分。

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.

有两种方法可以解决

1。伪造第1节的indexPath

这种方法有点麻烦,我不建议这样做。但是对于第1部分,您可以将 indexPath 设置为 indexPath.row,而不是为0 indexPath.row,indexPath.section (现在是第1行),使其表现得就像FRC本身在其自己的表视图中一样。

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:使用FRC初始化程序的 sectionNameKeyPath 参数设置不同的部分

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

此属性用于区分FRC的各个部分,以帮助支持多个部分。最好的例子是,如果您有一个名称字段,则可以使用 name 中的 sectionNameKeyPath 来按以下方式在表视图中显示部分:名称(例如设置了地址簿)。

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).

您似乎在尝试使用多个FRC在这里做一些小事/事,如果可能的话,可能是 BOOL 可以让您知道它是负数还是正数,因此可以将它们分为两个部分。这似乎不是最好的方法,因为您要在核心数据模型中添加一个新属性,除了区分部分外,不需要添加新属性,但有时在核心数据中需要新属性才能获得所需的功能。

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.

这篇关于为什么tableview的第1部分的indexPath以[1,0]开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆