简体   繁体   English

UITableView分组来自NSMutableArray的部分

[英]UITableView grouping sections from NSMutableArray

I have an app that basically reads an xml file and displays the results in a UITableView. 我有一个基本上读取xml文件的应用程序,并在UITableView中显示结果。 I am trying to group the list items by "country" (an attribute of the xml file elements) and display them in UITableView Sections. 我试图按“country”(xml文件元素的属性)对列表项进行分组,并将它们显示在UITableView Sections中。

Currently i read the xml file and store each Element as a custom object in an NSMutableArray. 目前我读取了xml文件并将每个Element存储为NSMutableArray中的自定义对象。 The array has the following structure: 该数组具有以下结构:

Array: 0 => (title, description, date, country) 1 => (title, description, date, country) 2 => (title, description, date, country) 3 => (title, description, date, country) 数组:0 =>(标题,描述,日期,国家)1 =>(标题,描述,日期,国家)2 =>(标题,描述,日期,国家)3 =>(标题,描述,日期,国家)

I have tried creating another array of unique countries which has allowed me to create the section headers correctly but i am struggling to work out a way to display the correct items beneath each section header. 我已经尝试创建另一个独特国家/地区阵列,这使我能够正确创建节标题,但我正在努力找到一种方法来显示每个节标题下面的正确项目。

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

Where itemCountry is the country attribute of each element as i loop through the xml file. 其中itemCountry是每个元素的country属性,因为我循环遍历xml文件。

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

So i guess my question is how do i work out how many rows need to go in each section? 所以我想我的问题是我如何确定每个部分需要进行多少行? How do display the correct array items for each section? 如何为每个部分显示正确的数组项?

Any help or pointers would be great 任何帮助或指针都会很棒

Rather than creating an array of custom objects containing your data, you should look at creating a dictionary. 您应该考虑创建字典,而不是创建包含数据的自定义对象数组。

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;

Later in numberOfSectionsInTableView : 稍后在numberOfSectionsInTableView

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

In tableView:numberOfRowsInSection: : tableView:numberOfRowsInSection: ::

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

In tableView:cellForRowAtIndexPath: : 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` */

    [..]
}

This should take you the whole way. 这应该带你一路走来。

Side Note 边注
If it weren't to present the data and just get the count of the countries then a better alternative to PengOne's approach is to use an NSCountedSet . 如果不提供数据并且只是获得国家的数量,那么PengOne的方法的更好的替代方案是使用NSCountedSet

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

Now all unique countries are available in [countedSet allObjects] and count for each country would be [countedSet countForObject:countryName] . 现在所有唯一的国家/地区都可以在[countedSet allObjects] ,每个国家/地区的计数将是[countedSet countForObject:countryName]

For the section headers using Deepak's answer just go: 对于使用Deepak答案的章节标题,请执行以下操作:

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

You should probably not create multiple arrays but create an array of dictionaries. 您可能不应该创建多个数组,而是创建一个字典数组。 So create a mutable array called counties or wtv and ads dictionaries that contain the title, name, etc. To create a dictionary it is quite easy just create an NSMutableDictionary object and then use the setValue:forKey: metod to add things to it. 因此,创建一个名为counties或wtv的可变数组和包含标题,名称等的广告字典。要创建字典,只需创建一个NSMutableDictionary对象然后使用setValue:forKey: metod来添加内容即可。 For the country name, the value would be the name and the key would by country. 对于国家/地区名称,值将是名称,密钥将按国家/地区。 Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM