2011-02-08 38 views
0

我想製作一個包含我們的商店的桌面視圖 - 按城市劃分。UITableView部分的動態標題

tableview的數據通過我的webapp的API收集在一個json字符串中,我使用開源的Objective-c JSON框架解析NSDictionary。

的反應看起來有點像這樣:

{ 
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.. 

這裏的問題:我們都相當迅速擴大,我不想更新我的應用程序每次我們在一個新的城市開店。所以我不能硬編碼城市的陣列。

什麼是正確的方式來確保新城市中的新店顯示在正確的城市內的表格視圖中?

在此先感謝!

編輯 這裏是我的代碼:

- (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,對嗎? – 2011-02-08 18:31:10

+0

糟糕,你是對的。編輯.. – 2011-02-08 18:46:12

回答

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

編輯

假設你只是想顯示每個店的標題,所有你需要的是如下:

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

這是把我的頭頂部,但一旦你有一個NSDictionary的數據您可以將其分類到一個NSSet中和那種字母。假設:

  • 你上市部分按字母順序排列
  • 圖書館您參考收益NSDictionaries的一個NSArray
  • 的鍵名,例如「城市」「代碼」「標題」

正確的數據確實需要是一個NSArray或NSSet的NSDictionary對象。如果是這樣的話:

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 ==鹿特丹

檢索所需的部分該行的最簡單的方法/問題電池是保證你的NSArray排在第一位。

當然,您需要更改執行排序的代碼,以將創建的數據分配給類屬性,而不是本示例中使用的本地創建的sortedSections。

注意事項:

  • 排序是大小寫敏感的,在數據​​會產生問題的不規則性。阿姆斯特丹!=阿姆斯特丹!=阿姆斯特丹等
+0

確實有點破碎:如果你可以檢查原因,P會很棒。旁邊:我沒有完全得到它:S。我有點看到你在做什麼,但是如何用`titleForHeaderInSection:`命名te部分,以及如何確保使用`cellForRowAtIndexPath:`顯示相應城市的正確商店? – 2011-02-08 19:38:52