gpt4 book ai didi

ios - 如何使用 Core Data 制作索引列表 TableView ,iOS Dev

转载 作者:行者123 更新时间:2023-11-29 03:55:46 27 4
gpt4 key购买 nike

我在核心数据中有一个名为“费用”的实体,其属性为“日期、类别、金额............”。如何根据日期属性中的“年-月”用索引列表列出所有费用?

从苹果的文档和教程中,我知道了如何制作索引列表表格 View 。但它需要提供“数组的数组”。这是所有部分的一个数组,以及一个特定部分中的对象的一个​​子数组。现在我的数据结构不是这样的。

根据我的想法,我能做的是:

  1. 先取出所有的费用,然后提取所有不重复的“年月”的情况,放入一个数组中,该数组是所有节的
  2. 然后我根据每个部分获取所有费用

但是我觉得这有点繁重,我可以更方便地实现这一点吗?或者我应该改变我的数据结构?谢谢你们:)

最佳答案

您可以创建一个仅返回给定范围内的费用的请求:

//Not tested
NSDateComponents* comps = [[NSDateComponents alloc] init];
NSCalendar* cal = [NSCalendar currentCalendar];
NSInteger year,month;//set these as you like
[comps setYear:year];
[comps setMonth:month];
NSDate* start = [cal dateFromComponents:comps];
month += 1;
month = (month <= 12 ? : 1);
[comps setMonth:month];
NSDate* end = [cal dateFromComponents:comps];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"date > %@ AND date < %@",start,end];
NSFetchRequest* request = [[NSFetchRequest alloc] initWithEntityName:@"Expense"];
[request setPredicate:predicate];

然后使用 NSFetchedResultsController 填充您的表格 View

关于ios - 如何使用 Core Data 制作索引列表 TableView ,iOS Dev,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16532814/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com