views:

73

answers:

1

Hi,

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.

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.

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.

A: 

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.

Daniel Dickison