【发布时间】:2009-06-15 18:20:42
【问题描述】:
我有一个 UITableView,其中有不同的部分 - 以字母“A”开头的单词转到“A”部分。以字母“B”开头的单词转到“B”部分,依此类推。
这是我的代码:
-(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];
}
我想要实现类似于 iPod 音乐列表的东西 - 有些歌曲按字母顺序排序,最有趣的部分是该列表支持除英语之外的所有其他语言。
我怎样才能做到这一点?我的代码仅适用于英文字母,所有其他字母都分配给最后一个部分。
这是我设置标题视图的方法:
- (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;
}
如何添加不同语言的代码支持?
提前谢谢你。
【问题讨论】:
标签: iphone cocoa-touch uitableview