如何使用多种语言制作节标题

时间:2022-04-11 15:04:19

I have an UITableView, and in it I have different sections - words that start with letter "A" go to the section "A". Words that start with letter "B" go to section "B" and so on.

我有一个UITableView,在其中我有不同的部分 - 以字母“A”开头的单词转到“A”部分。以字母“B”开头的单词转到“B”部分,依此类推。

Here is my code:

这是我的代码:

-(void) populateTable {
    if (tableDataArray)
    {
        [tableDataArray removeAllObjects];
    }

    for (int i = 0; i <= 27; i++)
    {
        NSMutableArray *words_in_section = [ [NSMutableArray alloc] init];
        [tableDataArray addObject: words_in_section];
        [words_in_section release];
    }

    int cur_section;
    int cur_word_id;

    //First section, without title
    while ( (cur_word_id = [ [WordsDatabase sharedWordsDatabase] getNextWordToEditIDABC]) != -1 )
    {
        NSMutableArray *temp_array = [tableDataArray objectAtIndex: 0];
        [temp_array addObject: [NSNumber numberWithInt: cur_word_id] ];
        [tableDataArray replaceObjectAtIndex: 0 withObject:temp_array];
    }

    //All other sections
    while ( (cur_word_id = [ [WordsDatabase sharedWordsDatabase] getNextWordIDABC]) != -1 )
    {
        cur_section = toupper([ [ [WordsDatabase sharedWordsDatabase] getWordAtID:cur_word_id] characterAtIndex:0 ] ) - 'A';
        if (cur_section < 0) cur_section = 27;
        else if (cur_section > 27) cur_section = 27;
        else cur_section++;

        NSMutableArray *temp_array = [tableDataArray objectAtIndex:cur_section];
        [temp_array addObject: [NSNumber numberWithInt: cur_word_id] ];
        [tableDataArray replaceObjectAtIndex:cur_section withObject:temp_array];
    }

    [mainTableView reloadData];
}

I want to achieve something similar to the iPod music list - there are songs which are sorted alphabetically and the most interesting part is that the list supports all other languages apart from english.

我想要实现类似于iPod音乐列表的东西 - 有按字母排序的歌曲,最有趣的部分是该列表支持除英语之外的所有其他语言。

How can I achieve this? My code works only with English letters and all other letters assigns to a last section.

我怎样才能做到这一点?我的代码仅适用于英文字母,所有其他字母分配给最后一部分。

Here is how I set the header views:

以下是我设置标题视图的方法:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if ([ [tableDataArray objectAtIndex: section] count] == 0) return nil;

    UITableViewCell *header_view = [ [ [UIView alloc] initWithFrame: CGRectMake(0, 0, 300, 20)] autorelease];

    header_view.backgroundColor = [UIColor orangeColor];

    UILabel *captionLabel = [ [ [UILabel alloc] initWithFrame: CGRectMake(10, 0, 290, 20)] autorelease];
    captionLabel.backgroundColor = [UIColor orangeColor];

    [header_view addSubview: captionLabel];

    if (section == 0)
    {
        return nil;
    } else if (section >= 27)
    {
        captionLabel.text = @"#";
        return header_view;
    } else
    {
        captionLabel.text = [NSString stringWithFormat: @"%c", section + 'A' - 1];
        return header_view;
    }

    return nil;
}

How can I add to my code support of different languages?

如何添加我的代码支持不同的语言?

Thank you in advance.

先感谢您。

1 个解决方案

#1


Different languages would have different conventions of how to categorize words. For instance, in Japanese you would probably want either 10 sections (for each row of the katakana table) or 50 or so to represent each katakana initial. I imagine you would have to special case the rules for each language.

不同的语言对于如何对单词进行分类会有不同的约定。例如,在日语中,您可能需要10个部分(对于片假名表的每一行)或50个左右来表示每个片假名首字母。我想你必须特殊情况下每种语言的规则。

#1


Different languages would have different conventions of how to categorize words. For instance, in Japanese you would probably want either 10 sections (for each row of the katakana table) or 50 or so to represent each katakana initial. I imagine you would have to special case the rules for each language.

不同的语言对于如何对单词进行分类会有不同的约定。例如,在日语中,您可能需要10个部分(对于片假名表的每一行)或50个左右来表示每个片假名首字母。我想你必须特殊情况下每种语言的规则。