-1

I have a grouped uitableview which has several sections. The sections are showing up fine and the number of rows is correct, however there is a problem with the positioning of the list items. I have an NSMutableArray with a list of names, I then split this array up using subArrayWithRange. Please see the code for more detail. The problem is that the list items are not appearing under the correct section, I can't seem to find the solution.

In cellForRowAtIndexPath:

NSInteger section = [indexPath section];
    NSString *sectionName = [sectionNamesArray objectAtIndex:section];
    NSCountedSet *set = [[NSCountedSet alloc] initWithArray:townArray];
    int numOfRows = [set countForObject:[NSString stringWithFormat:@"%@",sectionName]];
    int initNumRows = [set countForObject:[NSString stringWithFormat:@"%@",[sectionNamesArray objectAtIndex:0]]];

if(section == 0) {

    NSRange range = NSMakeRange(0,numOfRows);

    NSArray *subArray = [[namesArray subarrayWithRange:range] retain];
        NSArray *subTownArray = [[townArray subarrayWithRange:range]retain];
        //indexTracker = indexPath.row;

    //NSLog(@"sub array === %@, index Tracker = %i",subArray,indexTracker);
        cell.primaryLabel.text = [subArray objectAtIndex:indexPath.row];
        cell.secondaryLabel.text = [subTownArray objectAtIndex:indexPath.row];
    } else if (section == [sectionNamesArray count]-1) {

        int previousSection = section - 1;
        int previousNumRows = [set countForObject:[NSString stringWithFormat:[sectionNamesArray objectAtIndex:previousSection]]];
        NSRange range = NSMakeRange(previousNumRows+initNumRows,numOfRows);
        NSLog(@"previous num rows = %i and current num of rows = %i",previousNumRows,numOfRows);
        NSArray *subArray = [[namesArray subarrayWithRange:range] retain];
        NSArray *subTownArray = [[townArray subarrayWithRange:range]retain];
        cell.primaryLabel.text = [subArray objectAtIndex:indexPath.row];
        cell.secondaryLabel.text = [subTownArray objectAtIndex:indexPath.row];
        NSLog(@"sub array === %@",subArray);


    } else  {

        int previousSection = section - 1;


        int previousNumRows = [set countForObject:[NSString stringWithFormat:[sectionNamesArray objectAtIndex:previousSection]]];
        int location = previousNumRows;
        NSRange range = NSMakeRange(previousNumRows,numOfRows);
        NSLog(@"location = %i",location);
        NSLog(@"previous num rows = %i and current num of rows = %i",previousNumRows,numOfRows);
        NSArray *subArray = [[namesArray subarrayWithRange:range] retain];
        NSArray *subTownArray = [[townArray subarrayWithRange:range]retain];
        cell.primaryLabel.text = [subArray objectAtIndex:indexPath.row];
        cell.secondaryLabel.text = [subTownArray objectAtIndex:indexPath.row];

    }     //NSLog(@"section name = %@ . num of rows = %i",sectionName,numOfRows);





    return cell;
David Cahill
  • 419
  • 2
  • 5
  • 9

1 Answers1

1

I think you are over-complicating things. Why not create your sections (a Dictionary with just the section titles) with the data you have and then do something like this in the cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *selRow = [[self.units valueForKey:[[[self.units allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

...
}

Here is the method I use for building that Dictionary:

-(void)buildSectionKeysWithMutableArray:(NSMutableArray *)mutableArray {
    NSMutableString *prevUnit;
    units = [[NSMutableDictionary alloc] init];

    // Loop through the mutableArray passed in and build our keys
    for (NSDictionary *thisUnit in mutableArray)
    {
        NSString *unitName = [thisUnit objectForKey:@"LOCATION"];
        if (![prevUnit isEqualToString:unitName]) {
            [self.units setValue:[[NSMutableArray alloc] init] forKey:unitName];                
        }
        prevUnit = [NSString stringWithString:unitName];
    }

    // Loop again and add the keys into the units mutableDictionary
    for (NSDictionary *thisUnit in mutableArray)
    {
        [[self.units objectForKey:[thisUnit objectForKey:@"LOCATION"]] addObject:thisUnit];
    }

}

This allows you to keep the presentation of the data dynamic.

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
  • Just a note, the units NSMutableDictionary is a property in my ViewController. – LJ Wilson Jan 10 '12 at 11:41
  • At the moment section titles are in the townArray so do I create a new dictionary with the section titles and then add this to an nsmutablearray? – David Cahill Jan 10 '12 at 11:49
  • No just declare a towns NSMutableDictionary property and where my code says "units" change it to towns: The LOCATION key in the buildSectionKeysWithMutableArray method refers to the key in my array of NSDictionaries that contains the unit name. If you are still having trouble, send me an example of your array (just one array element) and I will change the example code to match what you need. – LJ Wilson Jan 10 '12 at 13:09