iphone如何使用sqlite获取组部分tableview的姓氏首字母?

发布于 2024-12-19 10:48:38 字数 1464 浏览 0 评论 0原文

我想知道如何为从 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++){
    //---获取每个状态的第一个字符---
    作者 *tempAuthor = [_authorsInfo objectAtIndex:i];
    char 字母 = [tempAuthor.name characterAtIndex:0];
    NSString *uniChar = [NSString stringWithFormat:@"%C", 字母表];

    //---将每个字母添加到索引数组---
    if (![nameIndex containsObject:uniChar])
    {            
        [nameIndex addObject:uniChar];
    }

更新*

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

//---创建索引---
nameIndex = [[NSMutableArray alloc] init];

for (int i=0; i<[_authorsInfo count]-1; i++){
    //---获取每个状态的第一个字符---
    作者 *tempAuthor = [_authorsInfo objectAtIndex:i];
    NSArray *nameComponents = [tempAuthor.name elementsSeparatedByString:@" "];
    NSString *lastName = [ nameComponents objectAtIndex: nameComponents.count-1 ];
    char 字母表 = [姓氏字符索引:0];


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

    //---将每个字母添加到索引数组---
    if (![nameIndex containsObject:uniChar])
    {            
        [nameIndex addObject:uniChar];
    }        
}

I would like to know how to make a alphabet group sections for the names extracted from sqlite database in iphone (xcode 4.2). I was able to do group section for first name but was unable to do the last name part. Some name have lowercase I want the headers letter to have all capital which includes the lower and uppercase letters of those names. Some names have 3 names and wanted to make sure i get the last name. My code is below and it seems to mix up the alphabet (from A to L to C to D) and also e and E (lower and upper which i dont want to display)

//---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];
    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];
    }

UPDATE*

self.authorsInfo = [poemDatabase database].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];
    }        
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2024-12-26 10:48:38

如果 tempAuthor.name 包含带有空格的完整名称,请尝试以下操作:

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

If tempAuthor.name contains whole name with spaces try this:

NSArray *nameComponents = [tempAuthor.name componentsSeparatedByString:@" "];
NSString *lastName = [ nameComponents objectAtIndex: nameComponents.count-1 ]
char alphabet = [lastName characterAtIndex:0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文