我正在開發一種費用跟蹤器類型的應用程序。我能夠使用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];
}
Thanks.Thats正是我需要的。 – ichanduu