2011-06-18 64 views
11

我有一個應用程序,基本上讀取一個XML文件,並在UITableView中顯示結果。我試圖按「country」(xml文件元素的屬性)對列表項進行分組,並將它們顯示在UITableView部分中。UITableView分組從NSMutableArray部分

當前我讀取xml文件並將每個元素作爲自定義對象存儲在NSMutableArray中。該陣列具有如下結構:

陣列: 0 =>(標題,描述,日期,國家) 1 =>(標題,描述,日期,國家) 2 =>(標題,描述,日期,國家) 3 =>(標題,描述,日期,國家)

我已經嘗試創建另一個允許我正確創建節標題的獨特國家數組,但我努力想辦法顯示每個部分標題下的正確項目。

if(![countryArray containsObject:itemCountry]) //if country not already in array 
{ 
    [countryArray addObject:itemCountry]; //Add NSString of country name to array 
} 

哪裏itemCountry是各元素的國家屬性作爲通過xml文件i循環。

[countryArray count]; //gives me the amount of sections needed 

所以我想我的問題是我如何計算每個部分需要去多少行? 如何顯示每個部分的正確數組項目?

任何幫助或指針將是巨大的

回答

21

與其創建一個包含數據的自定義對象數組,您應該查看創建的字典。

NSMutableDictionary * theDictionary = [NSMutableDictionary dictionary]; 

// Here `customObjects` is an `NSArray` of your custom objects from the XML 
for (CustomObject * object in customObjects) { 
    NSMutableArray * theMutableArray = [theDictionary objectForKey:object.country]; 
    if (theMutableArray == nil) { 
     theMutableArray = [NSMutableArray array]; 
     [theDictionary setObject:theMutableArray forKey:object.country]; 
    } 

    [theMutableArray addObject:object]; 
} 

/* `sortedCountries` is an instance variable */ 
self.sortedCountries = [[theDictionary allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

/* Save `theDictionary` in an instance variable */ 
self.theSource = theDictionary; 

後來在numberOfSectionsInTableView

- (NSInteger)numberOfSectionsInTableView { 
    return [self.sortedCountries count]; 
} 

tableView:numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [[self.theSource objectForKey:[self.sortedCountries objectAtIndex:section]] count]; 
} 

tableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    [..] 

    /* Get the CustomObject for the row */ 
    NSString * countryName = [self.sortedCountries objectAtIndex:indexPath.section]; 
    NSArray * objectsForCountry = [self.theSource objectForKey:countryName]; 
    CustomObject * object = [objectsForCountry objectAtIndex:indexPath.row]; 

    /* Make use of the `object` */ 

    [..] 
} 

這應該帶您WH ole的方式。

旁註
如果不是呈現數據和剛剛獲得國家的數量然後PengOne的做法是更好的選擇是使用NSCountedSet

NSCountedSet * countedSet = [NSCounted set]; 
for (NSString * countryName in countryNames) { 
    [countedSet addObject:countryName]; 
} 

現在所有獨特的國家都在[countedSet allObjects]提供和計數每個國家將[countedSet countForObject:countryName]

+0

@邁克爾讓我知道你是否需要進一步的幫助。 –

+0

工作得很好,謝謝 – Leo

+1

我該怎麼做章節標題? – Chrizzz

0

你可能不應該創建多個磁盤陣列,但創建詞典的數組。因此,創建一個名爲counties或wtv的可變數組,以及包含標題,名稱等的廣告詞典。創建詞典很容易,只需創建一個NSMutableDictionary對象,然後使用metod向其添加內容即可。對於國名,價值將是國家的名稱和關鍵。希望有所幫助。

+0

由Pete42建議將是一個我會使用的方法。通過這種方式,您可以使用KeyValueCoding對數組進行排序,檢索國家組等等(檢查Apple文檔中的valueForKeyPath:方法,它非常強大)。 –

3

對於使用Deepak的答案只是去的節標題:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    return [self.sortedCountries objectAtIndex:section]; 
}