2012-01-29 19 views

Trả lời

8

Điều bạn cần làm là tạo thuộc tính tạm thời trên đối tượng dữ liệu của mình và sau đó sắp xếp kết quả tìm nạp của bạn cho phù hợp. Đối với một TVGuide tôi đã làm việc trên, tôi cần phải sắp xếp kết quả theo airDay, và kết thúc sắp xếp các sự kiện bằng cách STARTDATE, và sử dụng tài sản thoáng cho phần chính con đường tên:

Trong Event.m:

-(NSString*) airDay 
{ 
    NSDateFormatter *dayFormatter=[[NSDateFormatter alloc] init]; 
    [dayFormatter setLocale:[NSLocale currentLocale]]; 
    [dayFormatter setDateStyle: NSDateFormatterMediumStyle]; 
    [dayFormatter setDoesRelativeDateFormatting: YES]; 

    return [dayFormatter stringFromDate:self.startDate]; 
} 

việc kết hợp fetchrequest

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:[Database db].managedObjectContext]; 
[fetchRequest setEntity:entity]; 

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: 
          [NSSortDescriptor sortDescriptorWithKey:@"startDate"   
                 ascending:YES], 
                 nil]; 

[fetchRequest setSortDescriptors:sortDescriptors]; 

NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                     managedObjectContext:[Database db].managedObjectContext 
                     sectionNameKeyPath:@"airDay" 
                       cacheName:@"SearchEvents"]; 
+0

'cacheName: @" SearchEvents "' Đây có phải là tên lớp hoặc thực thể hay không? Tôi gặp lỗi khi tôi tham chiếu Thực thể. Cảm ơn. –

+1

Thay vào đó, chỉ cần sử dụng nil làm cacheName. Trong nhiều trường hợp, bạn sẽ không cần cung cấp –

3

Vui lòng tham khảo mẫu mã Apple "DateSectionTitles", bạn có thể tìm kiếm này trong sự giúp đỡ Xcode. nó giúp rất nhiều !!

Các vấn đề liên quan