UITableView从NSMutableArray中分组节

12
我有一个应用程序,它基本上读取一个xml文件并在UITableView中显示结果。我试图按“国家”(xml文件元素的属性)对列表项进行分组,并在UITableView部分中显示它们。
目前,我读取xml文件并将每个元素作为自定义对象存储在NSMutableArray中。数组具有以下结构:
Array:   0 =>(标题,描述,日期,国家)   1 =>(标题,描述,日期,国家)   2 =>(标题,描述,日期,国家)   3 =>(标题,描述,日期,国家)
我尝试创建另一个唯一国家的数组,这使我能够正确创建部分标题,但我无法找到一种方法来在每个部分标题下正确显示相应项。
if(![countryArray containsObject:itemCountry]) //if country not already in array
{
   [countryArray addObject:itemCountry]; //Add NSString of country name to array
}

当我遍历XML文件时,itemCountry是每个元素的国家属性。

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

所以,我想问的是如何计算每个部分需要多少行?

如何显示每个部分的正确数组项?

任何帮助或指针都将非常感谢。

3个回答

22

与其创建一个包含数据的自定义对象数组,你应该考虑创建一个字典。

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` */

    [..]
}

这应该可以帮助你完成整个过程。

顺便说一下:
如果只需要获取国家数量而不是呈现数据,则使用NSCountedSet是比PengOne的方法更好的选择。

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

现在所有独特的国家都可以通过 [countedSet allObjects] 获得,而每个国家的计数则可以通过 [countedSet countForObject:countryName] 获得。


@Michael 如果你需要进一步的帮助,请告诉我。 - Deepak Danduprolu
1
我该怎么处理章节标题? - Christian Loncle
你如何在didselectpathforindexinrow方法中引用原始数组?比如说,你想要跳转到一个视图控制器,并使用该数组的数据。 - M Jesse
谢谢你。这太完美了 :) - Nick

3

对于使用Deepak的答案的部分标题,只需执行以下操作:

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

0
你可能不应该创建多个数组,而应该创建一个字典的数组。因此创建一个可变数组称为“counties”或其他名称,并添加包含标题、名称等信息的字典。创建一个字典很简单,只需创建一个NSMutableDictionary对象,然后使用setValue:forKey:方法将其添加到其中。对于国家名称,值将是名称,键将是国家。希望能有所帮助。

Pete42建议的方法是我会使用的方法。这样,您可以使用KeyValueCoding来对数组进行排序、检索国家组等等(请查看Apple文档中的valueForKeyPath:方法,它非常强大)。 - Wolfgang Schreurs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接