Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7041735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:04:55+00:00 2026-05-28T02:04:55+00:00

I have a grouped uitableview which has several sections. The sections are showing up

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:04:55+00:00Added an answer on May 28, 2026 at 2:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a grouped UITableView which displays a number of cells, which includes both
I have a grouped UITableView that has 3 sections. I would like to show
I have a grouped UITableView . It has 7 rows. I wanted a transparent
I have one problem with two sections in UITableview grouped style. First section having
I have a class which extends UITableView , and it has 4 sections. I
I have a grouped UITableView where not all sections may be displayed at once,
I have a grouped UITableView that contains several cells (just standard UITableViewCell s), all
I have a grouped UITableView with some large images. However, scrolling performance is quite
In my iPhone app, I have a (non-grouped) UITableView which uses all the bells
I have a grouped UITableView with some sections (with an header title). I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.