【问题标题】:display the returned values in section header在节标题中显示返回值
【发布时间】:2012-04-30 08:18:03
【问题描述】:

我正在开发一种费用跟踪器类型的应用程序。我能够使用 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];
}

【问题讨论】:

    标签: objective-c ios xcode core-data nsdate


    【解决方案1】:

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

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

    Apple 计算 primitive date 并在必须为节标题格式化字符串时重新构造它。一切都很简单。只需按照示例代码,您很快就解决了这个问题。

    【讨论】:

    • 谢谢。这正是我需要的。
    【解决方案2】:

    你可以使用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

    【讨论】:

    • 我能够按排序顺序接收日期。但是如何在节标题中显示它们以及如何在 tableView-noOfSections 方法中声明节数?
    • 这根本不能回答问题。
    • @Mundi 当我回答问题时,问题有所不同(OP 改进/更改了问题)。在发布答案前一小时查看所做的编辑。此外,OP 在该编辑中使用了我的建议。下次投反对票之前,请考虑在问题上花费更多时间。
    • @Mundi 甚至标题都被编辑了 :) 最初是:CoreData:按给定顺序从数据库中检索数据。所以我想我已经回答了最初的问题。
    猜你喜欢
    • 2021-07-27
    • 2015-08-12
    • 2020-02-02
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多