首页 文章

UITableView分组来自NSMutableArray的部分

提问于
浏览
11

我有一个基本上读取xml文件的应用程序,并在UITableView中显示结果 . 我试图按“country”(xml文件元素的属性)对列表项进行分组,并将它们显示在UITableView Sections中 .

目前我读取了xml文件并将每个Element存储为NSMutableArray中的自定义对象 . 该数组具有以下结构:

数组:0 =>( Headers ,描述,日期,国家)1 =>( Headers ,描述,日期,国家)2 =>( Headers ,描述,日期,国家)3 =>( Headers ,描述,日期,国家)

我尝试创建另一个独特的国家/地区阵列,这使我能够正确创建节 Headers ,但我正在努力找到一种方法来显示每个节 Headers 下面的正确项目 .

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

其中itemCountry是每个元素的country属性,因为我循环遍历xml文件 .

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

所以我想我的问题是我如何计算每个部分需要多少行?如何为每个部分显示正确的数组项?

任何帮助或指针都会很棒

3 回答

  • 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` */
    
        [..]
    }
    

    这应该带你一路走来 .

    Side Note
    如果不是't to present the data and just get the count of the countries then a better alternative to PengOne'的方法是使用 NSCountedSet .

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

    现在所有独特的国家/地区都可以在 [countedSet allObjects] 中找到,每个国家/地区的数量都是 [countedSet countForObject:countryName] .

  • 0

    对于使用Deepak答案的章节 Headers ,请转到:

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

    您可能不应该创建多个数组但是创建一个字典数组 . 因此,创建一个名为counties或wtv的可变数组和包含 Headers ,名称等的广告字典 . 要创建字典,只需创建一个 NSMutableDictionary 对象然后使用 setValue:forKey: metod向其中添加内容即可 . 对于国家/地区名称,值将是名称,密钥将按国家/地区 . 希望有所帮助 .

相关问题