1

我正在开发一种费用跟踪器类型的应用程序。我能够使用 coredata 完美地存储和检索排序数据。

我正在存储日期。我能够按排序顺序检索日期。但我想要节标题中的日期和该表中的相关数据。

例如:2011 年 12 月 31 日 ---------> 节标题
xxxxxxx yyyyyy -----> 带有标签的单元格

2012 年 1 月 1 日------> 部分标题
xxxxxxx yyyyyy ------> 带有标签的单元格。

我能够按排序顺序接收日期。但是如何按排序顺序在节标题中显示它们以及如何在 tableView-noOfSections 方法中声明节数?

//我用于检索数据的代码。

- (void)viewDidLoad
{

AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"NewExpense" inManagedObjectContext:context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSSortDescriptor *date = [[NSSortDescriptor alloc]initWithKey:@"date" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:date,nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setEntity:entityDesc];
NSError *error;
self.listOfExpenses = [[context executeFetchRequest:fetchRequest error:&error]retain]    [fetchRequest release];
[super viewDidLoad];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{    
return 1;// as of now i have taken one.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
[self.listOfExpenses count]; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //labels are created and added to the cell's subview.

NSManagedObject *records = nil;
records = [self.listOfExpenses objectAtIndex:indexPath.row];
self.firstLabel.text = [records valueForKey:@"category"];
NSString *dateString = [NSString stringWithFormat:@"%@",[records valueForKey:@"date"]];
NSString *dateWithInitialFormat = dateString;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
self.secondLabel.text = dateWithNewFormat;
NSString *amountString = [NSString stringWithFormat:@"%@",[records valueForKey:@"amount"]];
self.thirdLabel.text = amountString;
totalAmount = totalAmount + [amountString doubleValue];
}
4

2 回答 2

0

您可以使用NSSortDescriptor

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    ...
    NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:sortingKey ascending:ascending];
    NSArray * sortDescriptors = [NSArray arrayWithObject: sort];
    [request setSortDescriptors:sortDescriptors];

因为sortingKey你会通过你的情况date

请参阅NSSortDecsriptor此处的文档: https ://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html

于 2012-04-30T08:26:31.383 回答
0

部分标题中的日期有点棘手,但是 Apple 提供了非常好的示例代码,我在我当前的一个应用程序中成功使用了它。查看DateSectionTitles

简而言之,您需要使用 aNSFetchedResultsController并使用实体的单独属性定义sectionNameKeyPath(日期本身不会这样做,因为它精确到秒)。在numberOfSections你将不得不返回获取的结果控制器给你的任何东西,而不是1像上面那样。numberOfRowsInSection也必须进行调整,你明白了。

Apple 计算 aprimitive date并在必须格式化节标题的字符串时重新构建它。一切都很简单。只需按照示例代码进行操作,您就可以立即解决此问题。

于 2012-05-08T08:47:54.170 回答