【问题标题】:iphone how to grab first letter of last name for group section tableview using sqlite?iphone如何使用sqlite获取组部分tableview的姓氏的第一个字母?
【发布时间】:2011-12-04 09:27:29
【问题描述】:

我想知道如何为从 iphone (xcode 4.2) 中的 sqlite 数据库中提取的名称创建一个字母组部分。我能够为名字做组部分,但不能做姓氏部分。有些名字有小写我希望标题字母全部大写,包括这些名字的小写和大写字母。有些名字有 3 个名字,想确保我得到姓氏。我的代码在下面,它似乎混淆了字母表(从 A 到 L 到 C 到 D)以及 e 和 E(我不想显示的上下)

//---创建索引---

nameIndex = [[NSMutableArray alloc] init];

for (int i=0; i<[_authorsInfo count]-1; i++){
    //---get the first char of each state---
    authors *tempAuthor = [_authorsInfo objectAtIndex:i];
    char alphabet = [tempAuthor.name characterAtIndex:0];
    NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet];

    //---add each letter to the index array---
    if (![nameIndex containsObject:uniChar])
    {            
        [nameIndex addObject:uniChar];
    }

更新*

self.authorsInfo = [诗歌数据库].authorsInfo;

//---create the index---
nameIndex = [[NSMutableArray alloc] init];

for (int i=0; i<[_authorsInfo count]-1; i++){
    //---get the first char of each state---
    authors *tempAuthor = [_authorsInfo objectAtIndex:i];
    NSArray *nameComponents = [tempAuthor.name componentsSeparatedByString:@" "];
    NSString *lastName = [ nameComponents objectAtIndex: nameComponents.count-1 ];
    char alphabet = [lastName characterAtIndex:0];


    //char alphabet = [tempAuthor.name characterAtIndex:0];
    NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet];

    //---add each letter to the index array---
    if (![nameIndex containsObject:uniChar])
    {            
        [nameIndex addObject:uniChar];
    }        
}

【问题讨论】:

    标签: iphone ios header tableview


    【解决方案1】:

    如果tempAuthor.name 包含带有空格的全名,试试这个:

    NSArray *nameComponents = [tempAuthor.name componentsSeparatedByString:@" "];
    NSString *lastName = [ nameComponents objectAtIndex: nameComponents.count-1 ]
    char alphabet = [lastName characterAtIndex:0];
    

    【讨论】:

    • 它以不按顺序的随机字母显示,并且在我发布更新代码时也没有得到姓氏谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多