显示 UITableView 的本地化字母索引

Posted

技术标签:

【中文标题】显示 UITableView 的本地化字母索引【英文标题】:Show localized alphabetic index for UITableView 【发布时间】:2017-12-19 16:25:12 【问题描述】:

是否有的视图?这意味着如果我更改我的系统语言,索引标题也应该根据选择的语言进行更改,点击它们应该会将我带到 tableView 上的相应部分。

我对此相对较新,我在 S.O 附近徘徊,但只设法看到如何以特定语言显示它们,比如英语,但如果我更改我的系统语言,我希望我的索引标题也改变。

这就是我所做的 - 我有一个字母数组

@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#"]

我从那里回来的

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
    return alphabetArray;
 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
  // i go to respective index when i tap on some title
 

现在有人可以告诉我如何本地化它。例如韩国有这个系列

A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, ㄱ, ㄴ, ㄷ, ㄹ, ㅁ, ㅂ, ㅅ, ㅇ, ㅈ, ㅊ, ㅋ, ㅌ, ㅍ, ㅎ, #

我知道有类似的东西

[UILocalizedIndexedCollation currentCollation].sectionTitles.count . 

这会给我基于当前语言环境的标题计数,但我真的可以以某种方式使用它吗?有人可以解释一下这里可以做什么吗??

提前致谢!!

【问题讨论】:

alphabetArray 应基于单元格中显示的模型对象的显示字符串(如“标题”)创建。然后根据需要用韩文显示,应该这样做,不是吗? UILocalizedIndexedCollation 是要使用的正确类。但是您的应用程序必须本地化为您希望支持的任何语言。仅仅改变设备语言是不够的。 @maddy - 是的,我的应用程序已本地化,但我如何实现我上面提到的。我很困惑。 【参考方案1】:

使用UILocalizedIndexedCollation 是你想要的。

对于您所做的部分索引标题:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
    return [UILocalizedIndexedCollation currentCollation].sectionIndexTitles;

然后:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];

假设您希望节标题与节索引标题匹配,并且您的数据被分解为适当的节以匹配,您可以这样做:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    return [UILocalizedIndexedCollation currentCollation].sectionTitles[section];

【讨论】:

以上是关于显示 UITableView 的本地化字母索引的主要内容,如果未能解决你的问题,请参考以下文章

如何为 UITableView 索引事件添加操作 - 对于每个字母

iOS UITableView字母索引——系统自带检索栏

如何判断何时点击了 UITableView 部分索引字母?

更改索引组件的 UITableView “BackgroundColor”

将自定义对象的 NSArray 转换为按字母顺序划分的带有索引的 UITableView?

UITableView - 实现索引