Populate当有2个节时,来自CoreData的节中的行数 [英] Populate Number of rows in section from CoreData when there are 2 sections

查看:114
本文介绍了Populate当有2个节时,来自CoreData的节中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的CoreData中,我有一个Person实体,每个Person可以有多个(to Many)Statement实体。语句实体具有名为 amountOwed 的属性,它是一个十进制数。现在我的想法是循环所有的金额,并把它们加起来,如果他们是一个正数量将他们添加到正数组,如果他们是负数量添加到该数组。然后使用该数组来计算每个部分需要显示的单元格数。

In my CoreData, I have a Person entity, and each Person can have multiple (to Many) Statement entities. The statement entity has an attribute called amountOwed which is a decimal amount. Right now my idea is to loop over all the amounts and add them up, if they of a positive amount add them to the positive array and if they are negative amount add them to that array. Then use that array to figure out how many cells each sections needs to display.

我创建了一个 fetchedResultsController ,我试图将其用于for循环

I created a fetchedResultsController and I am trying to use that for the for loop

for i in 0..<fetchedResultsController.fetchedObjects!.count {

        let person = fetchedResultsController.fetchedObjects?[i]

        let amountTotal = person?.value(forKeyPath: "statement.@sum.amountOwed") as? Decimal

        if(amountTotal! <= Decimal(0) )
        {
            postiveCellNumber += 1
            print("\(postiveCellNumber) postive number count")
        }
        else{
            negativeCellNumber += 1
            print("\(negativeCellNumber) negative number count")
        }

    }

然后,我试图在 numberOfRowsInSection 这样的函数:

Then, I'm trying to use those arrays in the numberOfRowsInSection function like so:

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

    switch(section) {
    case 0:
        return postiveCellNumber
    case 1:
        return negativeCellNumber

    default :return 0
    }
}

我不认为我的for循环正在循环正常,因为我收到一个错误,指出

I don't think my for loop is looping properly as I get an error that says


在索引0处的索引2处没有对象。

no object at index 2 in section at index 0.


推荐答案

如何使用两个不同的查询,一个用于正值,一个用于负值,不同的提取结果控制器?然后你可以让FRC做你的迭代和计数。

How about using two different queries, one for the positive values and one for the negative ones, with two different fetched results controllers? Then you can let the FRC do the iterating and counting for you.

你不能使用FRCs来管理节。你必须自己这样做。为 sectionNameKeyPath 指定 nil 。然后你想要的东西像

You won't be able to use the FRCs to manage sections. You'll have to do that yourself. Specify nil for the sectionNameKeyPath. Then you want something like

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return positiveResultsController.fetchedObjects?.count ?? 0
    }
    else {
        return negativeResultsController.fetchedObjects?.count ?? 0
    }
}

或可能

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        let sectionInfo = positiveResultsController.sections![section]
        return sectionInfo.numberOfObjects
    }
    else {
        let sectionInfo = negativeResultsController.sections![section]
        return sectionInfo.numberOfObjects
    }
}


b $ b

tableView(_:cellForRowAt:)的类似逻辑。

这篇关于Populate当有2个节时,来自CoreData的节中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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