2011-12-04 5 views
2

iphone(xcode 4.2)のsqliteデータベースから抽出された名前のアルファベットグループセクションを作成する方法を知りたいと思います。私は名前のグループセクションを行うことができましたが、姓の部分を行うことができませんでした。ある名前には小文字があります。私は、ヘッダー文字にそれらの名前の大文字と小文字を含むすべての大文字を使用します。いくつかの名前は3つの名前があり、私は最後の名前を得ることを確認したかった。私のコードは下にあり、アルファベット(AからL、CからDまで)とeとE(表示したくない下位と上位)が混在しているようです。iphone sqliteを使用してグループセクションのテーブルビューの姓の最初の文字を取得する方法は?

// --- createインデックス---

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データベース] .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]; 
    }   
} 

答えて

2

tempAuthor.nameは、スペースで全体の名前が含まれている場合は、この試してみてください。それはランダムな文字でないために表示しても、私は更新コードのおかげで投稿としての最後の名前を取得していない

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

を – merrill

+0

こんにちは、私は何かを更新するかどうか疑問に思いますか? :) – merrill