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 3958058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:33:56+00:00 2026-05-20T02:33:56+00:00

I want to make a tableview containing our stores – sectioned by city. The

  • 0

I want to make a tableview containing our stores – sectioned by city.

The data for the tableview is collected thru my webapp’s API in a json string which I parse to a NSDictionary using the opensource Objective-c JSON framework.

The response looks a bit like this:

    {
city = "Amsterdam";
code = erbur;
title = "Shop Lorem";
},
{
city = "Amsterdam";
code = kadap;
title = "Shop Ipsum";
},
{
city = "Rotterdam";
code = elaml;
title = "Shop Dolor";
},
{
city = "Delft";
code = lamla;
title = "Shop Sit";
},
{
city = "Rotterdam";
code = koppa;
title = "Shop Amet";
},

My initial plan was to create one array per city, store those arrays in a dictionary and than, when naming the sections with titleForHeaderInSection: do something like this:

if (section == 0) {
    return @"Amsterdam"; }
else if (section == 1)  {
    return @"Roteterdam";
} Etc..

Here’s the problem: We are expanding quite rapidly and I don’t want to update my app everytime we open a shop in a new city. So I can’t hardcode the arrays for the cities.

What would be the right way to make sure that new stores in new cities are displayed in the table view within the right city?

Thanks in advance!

EDIT
Here’s my code:

 - (void)getLocData
    {

        SBJSON *parser = [[SBJSON alloc] init];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://foo.bar/locaties/lijst"]];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

        NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

        NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

        NSArray *statuses = [parser objectWithString:json_string error:nil];

        //the abive works

        [statusesArray release];
        statusesArray = statuses;


        NSArray *cityNames = [statuses valueForKey:@"code"];


    [locationCodes release];
    locationCodes = cityNames;


    - (void)viewDidLoad {
        [super viewDidLoad];

    [self getLocData];

        //titel
        self.title = NSLocalizedString(@"Locaties", @"Locaties - Steden");



    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

        return [locationCodes count];

    }


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return 1;

    }

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

        return [locationCodes objectAtIndex:section];

    }


    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }

        // Configure the cell...




        NSString *cityName = [locationCodes objectAtIndex:[indexPath section]];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"code like %@", cityName];

        NSArray *filteredShops = [statusesArray filteredArrayUsingPredicate:predicate];
        NSDictionary *currentShop = [filteredShops objectAtIndex:[indexPath row]];

        NSString *cellTitle = [currentShop objectForKey:@"title"];

        cell.textlabel.text = cellTitle;
        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-20T02:33:56+00:00Added an answer on May 20, 2026 at 2:33 am

    It looks as though your data ends up being an array of dictionaries. If that’s the case you can obtain an array of city names as follows:

    // Assuming you've stored the array in a variable named shops...
    NSArray *cityNames = [shops valueForKey:@"city"];
    

    So your tableView:numberOfSections: implementation could simply return a count of that array, and similarly tableView:titleForHeaderInSection: could just return the object in cityNames indexed by the section number.

    In tableView:cellForRowAtIndexPath:, you could then filter the array using an instance of NSPredicate:

    NSString *cityName = [cityNames objectAtIndex:[indexPath section]];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"city like %@", cityName];
    
    NSArray *filteredShops = [shops filteredArrayUsingPredicate:predicate];
    NSDictionary *currentShop = [filteredShops objectAtIndex:[indexPath row]];
    

    EDIT

    Assuming you just wanted to display each shop’s title, all you’d need is the following:

    NSString *title = [currentShop objectForKey:@"title"];
    // Assuming you've already obtained the cell...
    [[cell textLabel] setText:title];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a tableView like this: http://www.topappreviews101.com/ipappimg/4025/moma-ab-ex-ny-screenshot-3.jpg I created a horizontal table
I want to make an etag that matches what Apache produces. How does apache
I want to make a table in SqlServer that will add, on insert, a
I want to make a copy of an ActiveRecord object, changing a single field
I want to make an entity that has an autogenerated primary key, but also
I want to make a .NET Form as a TopMost Form for another external
I want to make sure people can't type the name of a PHP script
I want to make sure that a set of functions have the same signature
I want to make a JavaScript application that's not open source, and thus I
I want to make a login system using ASP.NET (MVC). On the internet, I

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.