Tôi đang cố gắng sử dụng FRC với dữ liệu ngôn ngữ hỗn hợp và muốn có chỉ mục phần.NSFetchedResultsController v.s. UILocalizedIndexedCollation
Nó có vẻ như từ các tài liệu bạn sẽ có thể ghi đè lên
- (NSString *)sectionIndexTitleForSectionName:(NSString *)sectionName
- (NSArray *)sectionIndexTitles
và của FRC sau đó sử dụng UILocalizedIndexedCollation để có một chỉ số và phần cục bộ. Nhưng thật đáng buồn điều này không hoạt động và không phải là những gì được dự định sẽ được sử dụng: (
Có ai có thể sử dụng FRC với UILocalizedIndexedCollation hoặc chúng tôi buộc phải sử dụng phương pháp sắp xếp thủ công được đề cập trong ví dụ UITableView + UILocalizedIndexedCollation ví dụ (mã ví dụ bao gồm nơi tôi đã làm việc này)
Sử dụng các thuộc tính sau
@property (nonatomic, assign) UILocalizedIndexedCollation *collation;
@property (nonatomic, assign) NSMutableArray *collatedSections;
và mã:.
- (UILocalizedIndexedCollation *)collation
{
if(collation == nil)
{
collation = [UILocalizedIndexedCollation currentCollation];
}
return collation;
}
- (NSArray *)collatedSections
{
if(_collatedSections == nil)
{
int sectionTitlesCount = [[self.collation sectionTitles] count];
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
collatedSections = newSectionsArray;
NSMutableArray *sectionsCArray[sectionTitlesCount];
// Set up the sections array: elements are mutable arrays that will contain the time zones for that section.
for(int index = 0; index < sectionTitlesCount; index++)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[newSectionsArray addObject:array];
sectionsCArray[index] = array;
[array release];
}
for(NSManagedObject *call in self.fetchedResultsController.fetchedObjects)
{
int section = [collation sectionForObject:call collationStringSelector:NSSelectorFromString(name)];
[sectionsCArray[section] addObject:call];
}
NSArray *sortDescriptors = self.fetchedResultsController.fetchRequest.sortDescriptors;
for(int index = 0; index < sectionTitlesCount; index++)
{
[newSectionsArray replaceObjectAtIndex:index withObject:[sectionsCArray[index] sortedArrayUsingDescriptors:sortDescriptors]];
}
}
return [[collatedSections retain] autorelease];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// The number of sections is the same as the number of titles in the collation.
return [[self.collation sectionTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// The number of time zones in the section is the count of the array associated with the section in the sections array.
return [[self.collatedSections objectAtIndex:section] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if([[self.collatedSections objectAtIndex:section] count])
return [[self.collation sectionTitles] objectAtIndex:section];
return nil;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self.collation sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [self.collation sectionForSectionIndexTitleAtIndex:index];
}
Tôi rất muốn vẫn có thể sử dụng giao thức FRCDelegate để được thông báo về các bản cập nhật. Nó có vẻ như không có cách nào tốt để làm cho hai đối tượng này làm việc cùng nhau độc đáo.
Tôi sẽ phải thử điều này trong lần tiếp theo tôi trình bày vấn đề này (giải pháp hiện có hoạt động tốt). Chỉ cần lưu ý: bạn không bao giờ nên sử dụng localizedCaseInsensitiveCompare vì một số ngôn ngữ (như tiếng Pháp tôi tin) đưa trường hợp vào tài khoản khi sắp xếp; sử dụng localizedStandardCompare. Điều này đã được đề cập trên video địa phương hóa WWDC mới nhất –
Trong tất cả các giải pháp tôi đã tìm thấy để phân loại và hiển thị dữ liệu được lập chỉ mục từ FRC, đây là cách thanh lịch nhất. Bạn đã tìm thấy một cách để di chuyển phần # đến cuối? Tôi cảm thấy giống như một trong các tùy chọn sắp xếp chuỗi sẽ làm điều này và kỳ lạ là phần của UILocalizedIndexedCollation'sIndexIndex được đặt hàng khác với thứ tự của bất kỳ bộ so sánh chuỗi nào sắp xếp theo. –
Đây không phải là một viên đạn bạc, vì vậy hãy cẩn thận. Là một ghi chú câu trả lời khác, bạn không thể xây dựng một 'NSFetchRequest' với một sắp xếp trên thuộc tính tạm thời như thuộc tính bạn đã mô tả. (Ít nhất, không phải nếu bạn đang sử dụng kho lưu trữ dữ liệu lõi được SQLite sao lưu.) Vì vậy, nếu thứ tự sắp xếp của bạn khác với thứ tự đối chiếu, 'NSFetchedResultsController' sẽ lỗi và không trả về hàng nào. Ví dụ: nếu họ của người dùng bắt đầu bằng chữ thường, nó sẽ được sắp xếp * sau * tất cả các tên khác, nhưng collation sẽ cố gắng đặt nó với các tên khác bắt đầu bằng cùng một chữ cái (nhưng viết hoa). – jsadler