2011-06-18 66 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]; 
}