2011-02-08 16 views
0

私たちの店舗を含むテーブルビューを作成したいと思います。UITableViewのセクションの動的タイトル

テーブルビューのデータは、JSONのJSONフレームワークを使用してNSDictionaryに解析するJSON文字列のwebappのAPIを通じて収集されます。

応答はビット次のようになります。

{ 
city = "Amsterdam"; 
code = erbur; 
title = "Shop Lorem"; 
}, 
{ 
city = "Amsterdam"; 
code = kadap; 
title = "Shop Ipsum"; 
}, 
{ 
city = "Rotterdam"; 
code = elaml; 
title = "Shop Dolor"; 
}, 
{ 
city = "Delft"; 
code = lamla; 
title = "Shop Sit"; 
}, 
{ 
city = "Rotterdam"; 
code = koppa; 
title = "Shop Amet"; 
}, 

私の当初の計画は titleForHeaderInSection:でセクションに名前を付けるときに、このような何かを、辞書にと比べて、それらの配列を格納、都市ごとに配列を作成することでした。

if (section == 0) { 
    return @"Amsterdam"; } 
else if (section == 1) { 
    return @"Roteterdam"; 
} Etc.. 

私たちはかなり急速に拡大しており、新しい都市で店を開くたびにアプリを更新したくありません。だから私は都市の配列をハードコードすることはできません。

新しい都市の新しい店舗が正しい都市のテーブルビューに表示されるようにする正しい方法はありますか?

ありがとうございます!

EDIT は、ここに私のコードです:

- (void)getLocData 
    { 

     SBJSON *parser = [[SBJSON alloc] init]; 

     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://foo.bar/locaties/lijst"]]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

     NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

     NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

     NSArray *statuses = [parser objectWithString:json_string error:nil]; 

     //the abive works 

     [statusesArray release]; 
     statusesArray = statuses; 


     NSArray *cityNames = [statuses valueForKey:@"code"]; 


    [locationCodes release]; 
    locationCodes = cityNames; 


    - (void)viewDidLoad { 
     [super viewDidLoad]; 

    [self getLocData]; 

     //titel 
     self.title = NSLocalizedString(@"Locaties", @"Locaties - Steden"); 



    } 
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

     return [locationCodes count]; 

    } 


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

     return 1; 

    } 

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

     return [locationCodes objectAtIndex:section]; 

    } 


    // Customize the appearance of table view cells. 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 

     // Configure the cell... 




     NSString *cityName = [locationCodes objectAtIndex:[indexPath section]]; 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"code like %@", cityName]; 

     NSArray *filteredShops = [statusesArray filteredArrayUsingPredicate:predicate]; 
     NSDictionary *currentShop = [filteredShops objectAtIndex:[indexPath row]]; 

     NSString *cellTitle = [currentShop objectForKey:@"title"]; 

     cell.textlabel.text = cellTitle; 
     return cell; 

    } 
+0

件名はUITableViewにする必要があります。 –

+0

おっと、そうです。編集.. –

答えて

3

あなたのデータは辞書の配列になっているように見えます。

// Assuming you've stored the array in a variable named shops... 
NSArray *cityNames = [shops valueForKey:@"city"]; 

だからあなたtableView:numberOfSections:の実装は、単にその配列の数を返すことができ、同様にtableView:titleForHeaderInSection:はちょうどセクション番号でインデックスさcityNamesでオブジェクトを返すことができます:その場合は次のように都市名の配列を取得することができます。 tableView:cellForRowAtIndexPath:

、あなたはその後、NSPredicateのインスタンスを使用してアレイをフィルタリングすることができます:

NSString *cityName = [cityNames objectAtIndex:[indexPath section]]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"city like %@", cityName]; 

NSArray *filteredShops = [shops filteredArrayUsingPredicate:predicate]; 
NSDictionary *currentShop = [filteredShops objectAtIndex:[indexPath row]]; 

EDIT

はあなたが必要とするだろうすべては、あなただけの各店舗のタイトルを表示するように望んでいたと仮定すると、以下:

NSString *title = [currentShop objectForKey:@"title"]; 
// Assuming you've already obtained the cell... 
[[cell textLabel] setText:title]; 
+0

大丈夫です!この例では、セルのテキストをどのように設定する必要がありますか? –

+0

それは私に範囲の例外を与えるでしょう。私は何かがフィルタリングで間違っていると推測している..任意の手がかり? –

+0

範囲例外とは何ですか?例外メッセージの状態は何ですか?あなたはもっと具体的になりますか? – jlehr

0

これは私の頭の上からですが、あなたはNSDictionaryの内のデータを持っていたら、あなたはNSSetにそれをソートし、ソートそのアルファベット順でした。仮定:

  • あなたはキーは、例えば、名前であるあなたが戻って参照ライブラリNSDictionaries
  • のNSArrayの
  • アルファベット順にセクションをリストアップしています"city" "code" "title"

右のデータは、NSDictionaryオブジェクトのNSArrayまたはNSSetである必要があります。もしそうなら、これは動作します:

NSMutableSet *sectionsSet = [[NSMutableSet alloc] init]; 
for (NSDictionary *item in data) { 
    NSDictionary *aLocation = [[NSDictionary alloc] initWithObjectsAndKeys:[item valueForKey:@"city"],@"city",nil]; 
    [sectionsSet addObject:aLocation]; 
    [aLocation release]; 
} 

NSSortDescriptor *sortAscending = [[NSSortDescriptor alloc] initWithKey:@"city" ascending:YES]; 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortAscending,nil]; 
NSArray *sortedSections = [[sectionsSet allObjects] sortedArrayUsingDescriptors:sortDescriptors]; 
[sortDescriptors release]; 
[sortAscending release];  

上記はNSDictionaryのダミーNSArrayでテストされています。 NSDictionaryにオブジェクトを追加すると、NSSortDescriptorを独自に作成する必要がなくなります。:/後でこのコードをチェックし、壊れていれば更新します。

titleForHeaderInSectionコードは、単に次のようになります。

リターン[sortedSections objectAtIndex:セクション]。

都市がアルファベット順にソートされているのを見ると、上記のコードではそれらの順番で返されます。あなたが質問にセクション/セルのために必要な行を取得する最も簡単な方法は、確実にするためである

0 ==アムステルダム 1 ==デルフト 2 ==ロッテルダム

:あなたの例のためには、次の3つのセクションを与えるだろうNSArrayは最初に正しくソートされます。

もちろん、この例で使用したローカルに作成されたsortedSectionsではなく、作成したデータをクラスプロパティに割り当てるソートを行うコードを変更する必要があります。

警告:

  • ソートは、データの不規則性に問題が生成されます、大文字と小文字が区別されます。アムステルダム!=アムステルダム!=アムステルダムなど
+0

それは確かに少し壊れています:Pあなたがなぜチェックすることができたらすばらしいでしょう。それの次に:私はそれを完全には得ていない:S。私はちょっとあなたが何をしているのか見ていますが、私は 'titleForHeaderInSection:'でteセクションの名前をどのように指定しますか? 'cellForRowAtIndexPath:'で対応する都市の正しいストアが表示されるようにするにはどうすればいいですか? –

+0

はい、そうではありませんでした。今更新しています! – Diziet

+0

あなたのためにそれを更新しました。あなたの質問からは明らかではありませんが、NSDictionaryオブジェクトのNSArrayを扱っていませんか? – Diziet

関連する問題