2017-01-25 1 views
0

これは初めての質問です。 私はこのようになりますJSONデータファイルを持っている:jSONファイルをアルファベット順にソートし、それを対応する文字に割り当てる方法sectionInSourceTableView/sectionForSectionIndexTitle

[ { 
"category": "Feline", 
"text": "CAT", 
"definition": "Meow" }, 
{ 
"category": "Mammal", 
"text": "DOG", 
"definition": "Bark" }, 
{ 
"category": "Insect", 
"text": "ANT ", 
"definition": "buzz" }, 
{ 
"category": "Insect'", 
"text": "MOSQUITO ", 
"definition": "buzz" }, 
{ 
"category": "Number'", 
"text": "10", 
"definition": "ten" } 
] 

次のように私はそれを輸入しています、データリストはMutableArrayを使用することが良好であれば、私も混乱していたり​​、私が使用してする必要がある場合は、NSMutableArrayのですNSDictionaryの?:

-(void)readDataFromFile { 

    NSString * filePath =[[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"]; 

    NSError * error; 
    NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; 

    if(error) 
    { 
     NSLog(@"Error reading file: %@",error.localizedDescription); 
    } 
    self.dataList = (NSMutableArray *)[NSJSONSerialization 
           JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding] 
           options:0 error:NULL]; 

} 

私は「テキスト」 とも私はセクションとインデックスリストを使用していますに従ってアルファベット順にJSONデータをソートしたいと思います。私は主にチュートリアルhttps://www.appcoda.com/ios-programming-index-list-uitableview/に続いた。

JSONデータをソートする方法や、アルファベットのセクションとiOSの連絡先アカウントを「#」セクションに入れて分ける方法が混乱しています。 これもどのように達成できますか?

ありがとうございます!

+0

チェックアウト[UILocalizedIndexedCollat​​ion](https://developer.apple.com/reference/uikit/uilocalizedindexedcollat​​ion)、(それは使うには少し面倒ですが)その目的のために設計されています。 – pbasdf

+0

これでJSONファイルをどうやって使うことができますか? – user1531

+1

「dataListはNSMutableArrayです」、 'self.dataList =(NSMutableArray *)[NSJSONSerialization JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]オプション:0エラー:NULL];' NSArray'でなければなりません。 – Larme

答えて

0

In the below code i've made an 2D array of 27 elements(1 for numeric and 26 for alphabets) and then placed the value according to the string initial character. This piece of code worked for me.Hope so it will work for you too :)

 for (int section = 0; section < 27; section++) 
     { 
      [arrSections addObject:[NSMutableArray array]]; 
     } 
     for (NSDictionary *item in SampleDictionary) 
     { 
      NSString *KeyValue = [item[@"DictionaryKEy"] capitalizedString];      
      *//In your case its Text* 
      int ascii = (int)[KeyValue characterAtIndex:kZero]; 

      if (ascii >= 65 && ascii <= 90) 
      { 
       [arrSections[(ascii - 64)] addObject:item]; 
      } 
      else 
      { 
       [arrSections[0] addObject:item]; 
      } 
     } 
+0

定数kZeroを0に使うのはばかげている。 – gnasher729

関連する問題