2012-04-30 18 views
1

我正在開發一種費用跟蹤器類型的應用程序。我能夠使用coredata完美地存儲和檢索已排序的數據。在部分標題中顯示返回的值

我存儲date.i能夠檢索排序的日期order.but我想在該表中的部分標題和相關數據的日期。

例如: 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]; 
} 

回答

0

日期節頭是有點棘手,但來自蘋果很好的示例代碼在那裏,我成功地用在我目前的應用程序之一它。看看DateSectionTitles

簡而言之,您需要使用NSFetchedResultsController並使用您的實體的單獨屬性(日期本身不會執行,因爲它準確到第二個)來定義sectionNameKeyPath。在numberOfSections中,您將不得不返回提取的結果控制器爲您提供的任何內容,而不是上述的1numberOfRowsInSection也必須調整,你明白了。

Apple計算一個primitive date,並在必須格式化部分標題的字符串時重新構建它。所有非常簡單。只需按照示例代碼進行操作即可解決問題。

+0

Thanks.Thats正是我需要的。 – ichanduu

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

+0

我能夠接收排序順序的日期,但如何在節標題中顯示它們以及如何聲明tableView-noOfSections方法中的節數? – ichanduu

+0

這根本不回答問題。 – Mundi

+0

@Mundi當我回答時,問題是不同的(OP改進/改變了問題)。在發佈答案前一小時查看編輯。此外,OP在編輯中使用了我的建議。下次投票前,請考慮花更多時間解決問題。 –