【问题标题】:UITableView grouping sections from NSMutableArray来自 NSMutableArray 的 UITableView 分组部分
【发布时间】:2011-06-18 16:34:47
【问题描述】:

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

目前我读取 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 文件时每个元素的国家属性。

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

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

任何帮助或指针都会很棒

【问题讨论】:

    标签: iphone ios uitableview nsmutablearray


    【解决方案1】:

    您应该考虑创建字典,而不是创建包含数据的自定义对象数组。

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

    这应该会带你一路走来。

    旁注
    如果不是提供数据而只是获取国家/地区的数量,那么 PengOne 方法的更好替代方法是使用NSCountedSet

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

    现在[countedSet allObjects] 中提供了所有唯一国家/地区,每个国家/地区的计数将是[countedSet countForObject:countryName]

    【讨论】:

    • @Michael 如果您需要进一步的帮助,请告诉我。
    • 节标题怎么办?
    • 如何引用 didselectpathforindexinrow 中的原始数组?比如说,您想使用该数组中的信息推送一个视图控制器
    • 谢谢。这是完美的:)
    【解决方案2】:

    对于使用 Deepak 答案的部分标题,只需:

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

    【讨论】:

      【解决方案3】:

      您可能不应该创建多个数组,而是创建一个字典数组。因此,创建一个名为counties 或wtv 的可变数组和包含标题、名称等的ads 字典。要创建字典非常简单,只需创建一个NSMutableDictionary 对象,然后使用setValue:forKey: 方法向其中添加内容。对于国家/地区名称,值将是名称,键将按国家/地区。希望有帮助。

      【讨论】:

      • Pete42 建议的方法是我会使用的方法。这样,您可以使用 KeyValueCoding 对数组进行排序、检索国家组等...(查看 Apple 文档中的 valueForKeyPath: 方法,它非常强大)。
      猜你喜欢
      • 1970-01-01
      • 2011-12-14
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      相关资源
      最近更新 更多