2014-03-26 33 views
1

我有一個UITableViewController,它使用NSFetchedResultsController顯示一個列表。 這是myObject的列表,並有一個屬性date在numberOfSectionsInTableView中使用NSFetchedResultsController對日期進行分組

我創建了一個方法- (NSFetchedResultsController *)fetchedResultsController以獲取按日期排序的所有條目。

結果是這樣的: enter image description here

這個我已經加入了- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section讓時間+日期將在dateStyle:NSDateFormatterMediumStyle顯示後。

如下圖所示,您可以看到日期打印正確,但2014年3月12日的2行和2014年3月14日的2行沒有分組在一起。

enter image description here

我的猜測是,我需要改變numberOfSectionsInTableView方法,因爲它仍然在尋找的日期+時間,而不能僅把日期,但我不知道怎麼樣。

我的代碼:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     NSInteger count = [[self.fetchedResultsController sections] count]; 
     return count; 
    } 

一種可能性是將屬性添加到我的對象,如「sectionIdentifier」只在其中存儲日期和使用。我想知道是否有人有另一個想法如何解決這個問題。是否可以將這些日期分組在一起,我的代碼添加到- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

+0

你可能想補充一點,擁有你想要同時保持實際日期存儲格式的日期基於'NSString'一個截面屬性作爲'NSDate' – Volker

+0

我不確定你的意思,我試圖添加到NSString,但這是行不通的。如果我使用的NSLog我得到這個[_bds fetchedResultsController]節]( 「<_NSDefaultSectionInfo:0x1097ce5d0>」, 「<_NSDefaultSectionInfo:0x1097d0230>」, 「<_NSDefaultSectionInfo:0x1097d0b20>」, 「<_NSDefaultSectionInfo:0x1097ceca0>」, 「<_NSDefaultSectionInfo:0x1097cff00>」, 「<_NSDefaultSectionInfo:0x1097d0010>」, 「<_NSDefaultSectionInfo:0x1097d0600>」, 「<_NSDefaultSectionInfo:0x1097d0790>」, 「<_NSDefaultSectionInfo:0x1097d1770>」, 「<_NSDefaultSectionInfo: 0x1097d1370>「 – Eloy

+0

您的數據模型需要擴展爲具有字符串... – Volker

回答

1

我喜歡用FRC控制它,如果他們想要不同/自定義功能,讓委託調用改變它。我也相信FRC一般應該作爲被觀察實體的分類方法來構建。

您可以格式化字符串是全對對象的屬性,但似乎浪費。

你也可以把它變成一個真正的瞬態特性。這可能更合適,但我使用的是現有模型,並不想更改模型。從那個項目開始,我就把這個方法學應用於其他類似的案例。

無論如何,本實施例中幾乎是直的從現有的應用程序截取。我不得不改變一些東西,所以我希望我沒有遺漏任何東西。

創建FRC使用的提取請求,以便根據對象的date屬性進行排序。這允許在標準屬性上獲取正常的核心數據。

+ (NSFetchedResultsController *) 
    fetchedResultsController:(NSManagedObjectContext *)context 
{ 
    NSFetchRequest* request = [NSFetchRequest 
     fetchRequestWithEntityName:[self entityName]]; 
    request.sortDescriptors = @[[NSSortDescriptor 
     sortDescriptorWithKey:@"date" 
        ascending:YES]]; 
    request.fetchBatchSize = 30; 
    request.returnsObjectsAsFaults = NO; 

    NSString *cacheName = [[self entityName] 
     stringByAppendingString:@"-all-bydate"]; 
    return [[NSFetchedResultsController alloc] 
     initWithFetchRequest:request 
     managedObjectContext:context 
      sectionNameKeyPath:@"dateAsSectionName" 
        cacheName:cacheName]; 
} 

一個實例變量添加到您的子類的......你會管理它自己...

@implementation MyManagedObjectSubclass { 
    NSString *dateAsSectionName_; 
} 

添加的存取方法。我們只創建一次格式化程序。改變它你喜歡的。

- (NSString*)dateAsSectionName 
{ 
    static NSDateFormatter *dateFormatter; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     dateFormatter = [[NSDateFormatter alloc] init]; 
     dateFormatter.dateFormat = @"EEE, MMM d, h:mm a"; 
     dateFormatter.dateFormat = @"EEEE MMMM d"; 
    }); 

    if (dateAsSectionName_ == nil) { 
     NSDate *date = [self primitiveDate]; 
     dateAsSectionName_ = [dateFormatter stringFromDate:date]; 
    } 
    return dateAsSectionName_; 
} 

當日期發生變化時清除我們的部分名稱,所以我們將在下次訪問者被調用時重新計算它的值。

- (void)setDate:(NSDate*)date 
{ 
    if ([[self primitiveDate] isEqualToDate:date]) return; 

    [self willChangeValueForKey:@"date"]; 
    [self setPrimitiveDate:date]; 
    [self didChangeValueForKey:@"date"]; 

    dateAsSectionName_ = nil; 
} 

告訴志願是dateAsSectionName取決於date

+ (NSSet *)keyPathsForValuesAffectingDateAsSectionName 
{ 
    return [NSSet setWithObject:@"date"]; 
} 
0

既讀取後,添加屬性似乎是答案

相關問題