1

I have an array:

 NSArray *nameArray = [NSArray arrayWithObjects:
                     //A
                     @"Alpha",@"Ar",@"Ax",
                     //B
                     @"B", ... nil];

In my viewDidLoad I do the following:

  _sectionedNameArray = [self partitionObjects:nameArray collationStringSelector:@selector(self)];

Here's the partitionObjects method:

-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector

{
_collation = [UILocalizedIndexedCollation currentCollation];

NSInteger sectionCount = [[_collation sectionTitles] count]; //section count is take from sectionTitles and not sectionIndexTitles

NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];

//create an array to hold the data for each section
for(int i = 0; i < sectionCount; i++)
{
    [unsortedSections addObject:[NSMutableArray array]];
}

//put each object into a section
for (id object in array)
{
    NSInteger index = [_collation sectionForObject:object collationStringSelector:selector];
    [[unsortedSections objectAtIndex:index] addObject:object];
}

NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];

//sort each section
for (NSMutableArray *section in unsortedSections)
{
    [sections addObject:[_collation sortedArrayFromArray:section collationStringSelector:selector]];
}

return sections;
}

and the following:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[_sectionedNameArray objectAtIndex:section] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[_collation sectionTitles] objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [_collation sectionIndexTitles]; 
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}

Issue is:

The sections are not working correctly. I get "A" for section "A" then randomly divided "A"s for the other sections. It looks like each section is counting back from the beginning of the array... Any ideas what's wrong with my code?

Update following comment from rmaddy:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"hCell";

NameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

cell.hNameLabel.text = [nameArray objectAtIndex:indexPath.row];
cell.hCityLabel.text = [cityArray objectAtIndex:indexPath.row];
cell.hPoneLabel.text = [phoneArray objectAtIndex:indexPath.row];

return cell;
}
rkh
  • 841
  • 12
  • 29
  • Post your code for the `cellForRowAtIndexPath:` method. It sounds like you always grab the data from section 0 instead of the index path's section. – rmaddy Dec 09 '13 at 02:20
  • Updated above at the end of the post. – rkh Dec 09 '13 at 02:25
  • 1
    OK, what are `nameArray` and the others? Why aren't you accessing the data from `_sectionedNameArray`? You need section-specific data. – rmaddy Dec 09 '13 at 02:27
  • I think I understand why the issue is now. nameArray was the original array before I added the indexes. City and Phone Arrays are two different arrays corresponding to the nameArray. For example alpha has city=X defined in cityArray and phone=Y defined in phoneArray... sizeOf(nameArray) == sizeOf(cityArray) == sizeOf (phoneArray)... However if I use _sectionedNameArray to deduce hNameLabel.text then should I get sectionedCityArray and sectionedPhoneArray for the whole thing to work or is there another way? – rkh Dec 09 '13 at 02:30
  • 1
    All of your data needs to be broken up into an array of section data. You shouldn't have so many arrays. Just one - the top level section array. That array should have dictionaries where each dictionary has data specific to that section. A key for the section title, a key for the array of data in section. The array of data for the given section should be whatever you need to hold the data for each row. A class with several properties is better than a set of independent arrays. – rmaddy Dec 09 '13 at 02:34
  • thanks! Will update accordingly! – rkh Dec 09 '13 at 02:36
  • I ended up moving to dictionary (never used it before, had to learn it)... Would appreciate it if you can, as an answer to my question, simply explain the difference between the selector (self/description/etc.) and I'll mark that as an answer to give you proper credit. Thanks in advance! – rkh Dec 09 '13 at 23:08

0 Answers0