2009-07-03 87 views
7

我有一個NSFetchedResultsController正在從NSManagedObjectContext獲取對象。我正在使用結果填充UITableView。從NSFetchedResultsController設置UITableView標題

我正在過濾這兩個排序描述符。

NSSortDescriptor *lastOpened = 
    [[NSSortDescriptor alloc] initWithKey:@"lastOpened" ascending:NO]; 

NSSortDescriptor *titleDescriptor = 
    [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES]; 

當我創建NSFetchedResultsController,我通過sectionNameKeyPath:@"lastOpened"切片進行排序。

現在我的部分顯示標準格式,如2009-07-02 20:51:27 -0400由於沒有兩個可以同時打開,它們都是唯一的。我需要它們來涵蓋日期/時間的範圍,例如一整天,並且以人類可讀的形式。類似於7月2日星期四

謝謝!


編輯:

這畢竟是一個UITableViewController內。這裏有更多的代碼。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    // Display the dates as section headings. 
    return [[[fetchedResultsController sections] objectAtIndex:section] name]; 
} 



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects]; 
} 
+0

您是否使用綁定來填充表或您是否爲UITableView編寫了數據源? – 2009-07-03 02:13:59

回答

12

我最終在我的NSManagedObject子類中添加了一個新的屬性day以獲取格式化的日期字符串。

@property (nonatomic, readonly) NSString * day; 
@property (nonatomic, retain) NSDateFormatter * dateFormatter 

確定dateFomatter。 @synthesize dateFormatter;

我在awakeFromFetch和awakeFromInsert中初始化日期格式化程序。

self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 

day的訪問器。

- (NSString *)day { 
    NSDate *date = self.startDate; 
    return [self.dateFormatter stringFromDate:date]; 
} 

然後我設置我的部分名稱keypath看day;您不需要對您的排序描述符進行任何更改。

0

我還沒有機會深入到SDK的那個區域,但(我的工作是用C#,所以iPhone的東西是我的愛好的時間),但我可以告訴你會想使用另一個init,特別是initWithKey:ascending:selector:並且對於選擇器,您可以將選擇器傳遞給比較方法,該比較方法僅在年/月/日進行比較,而忽略時間。

這裏的網址以供參考:iPhone Library Documentation

希望這有助於!