Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Rather than creating an array of custom objects containing your data, you should look at creating a dictionary.</p> <pre><code>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; </code></pre> <p>Later in <code>numberOfSectionsInTableView</code>:</p> <pre><code>- (NSInteger)numberOfSectionsInTableView { return [self.sortedCountries count]; } </code></pre> <p>In <code>tableView:numberOfRowsInSection:</code>:</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[self.theSource objectForKey:[self.sortedCountries objectAtIndex:section]] count]; } </code></pre> <p>In <code>tableView:cellForRowAtIndexPath:</code>:</p> <pre><code>- (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` */ [..] } </code></pre> <p>This should take you the whole way.</p> <p><strong>Side Note</strong><br> 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 <code>NSCountedSet</code>.</p> <pre><code>NSCountedSet * countedSet = [NSCounted set]; for ( NSString * countryName in countryNames ) { [countedSet addObject:countryName]; } </code></pre> <p>Now all unique countries are available in <code>[countedSet allObjects]</code> and count for each country would be <code>[countedSet countForObject:countryName]</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload