iphone - UILocalizedIndexedCollat​​ion 是如何工作的? (文档中的示例代码)

标签 iphone objective-c uitableview

UILocalizedIndexedCollat​​ion 是如何工作的?

我正在阅读 simpleIndexedTableView sample code根据文档,RootViewController 已初始化,我们向其发送一些数据(rootViewController.timeZonesArray = timeZones;),但此时,collat​​ion 是已在 View Controller 中使用:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // The number of sections is the same as the number of titles in the collation.
    return [[collation sectionTitles] count];
}

基本上这就是魔法似乎发生的地方,那么 [collat​​ion sectionTitles] 包含什么?某些东西必须是自动的,但我不明白整个事情是如何运作的?

- (void)configureSections {

    // Get the current collation and keep a reference to it.
    self.collation = [UILocalizedIndexedCollation currentCollation];

    NSInteger index, sectionTitlesCount = [[collation sectionTitles] count];

    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];

    // Set up the sections array: elements are mutable arrays that will contain the time zones for that section.
    for (index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [newSectionsArray addObject:array];
        [array release];
    }

    // Segregate the time zones into the appropriate arrays.
    for (TimeZoneWrapper *timeZone in timeZonesArray) {

        // Ask the collation which section number the time zone belongs in, based on its locale name.
        NSInteger sectionNumber = [collation sectionForObject:timeZone collationStringSelector:@selector(localeName)];

        // Get the array for the section.
        NSMutableArray *sectionTimeZones = [newSectionsArray objectAtIndex:sectionNumber];

        //  Add the time zone to the section.
        [sectionTimeZones addObject:timeZone];
    }

    // Now that all the data's in place, each section array needs to be sorted.
    for (index = 0; index < sectionTitlesCount; index++) {

        NSMutableArray *timeZonesArrayForSection = [newSectionsArray objectAtIndex:index];

        // If the table view or its contents were editable, you would make a mutable copy here.
        NSArray *sortedTimeZonesArrayForSection = [collation sortedArrayFromArray:timeZonesArrayForSection collationStringSelector:@selector(localeName)];

        // Replace the existing array with the sorted array.
        [newSectionsArray replaceObjectAtIndex:index withObject:sortedTimeZonesArrayForSection];
    }

    self.sectionsArray = newSectionsArray;
    [newSectionsArray release]; 
}

谢谢你的帮助

最佳答案

[collat​​ion sectionTitles] 仅包含标准字母表排序,以便沿 tableview 的右侧显示。即使您只有以“B”和“F”开头的项目,侧面的索引也会始终显示完整的字母表。但由于用于对字符串进行排序的标准字母表会根据区域设置而变化,Apple 为我们提供了 UILocalizedIndexedCollat​​ion 以便轻松向用户展示他们的期望。

关于iphone - UILocalizedIndexedCollat​​ion 是如何工作的? (文档中的示例代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10177637/

相关文章:

iphone - UIImageWriteToSavedPhotosAlbum 未在左下角显示缩略图

ios - 旋转后键盘延伸高度不正确

苹果手机 : NSXMLParser fails to identify HTML special entity &amp;

ios - 使用 iOS 7 中的动态类型,我如何考虑更高级的布局问题,例如标签/ View 之间的间距?

ios - 如何将 "Log In via Twitter"凭据从 Rails 后端传递到 iPhone 应用程序?

ios - UISegmentedControl 在 View Controller 之间切换

iphone - 如何使用C语言向数据库中插入数据?

ios - 对具有自己的转换的 ViewController 的不平衡调用

iphone - UITableView 中的文本字段

ios - AccessibilityElements 在 TableView 中不起作用