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

  • Home
  • SEARCH
  • 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 8677311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:25:30+00:00 2026-06-12T20:25:30+00:00

I am sorting my tableview by Distributor using the code below (it was Alphabetical

  • 0

I am sorting my tableview by Distributor using the code below (it was Alphabetical by product)

    NSSortDescriptor *aSort =[[NSSortDescriptor alloc] initWithKey:@"Dis" ascending:YES];
    [distribArray sortUsingDescriptors:[NSMutableArray arrayWithObject:aSort]];

    NSLog( @"data from table %@", distribArray);

    [self.tableView reloadData];


    NSLog(@"ok2222222222");
    [[NSUserDefaults standardUserDefaults] setValue:@"Dis" forKey:@"ListBy"];
    [[NSUserDefaults standardUserDefaults] synchronize];

I would like to know what the easiest way to display the Distributor name as a Title header above all the products for that Distributor. I currently display the Distributor name in the DetailsView of the cell for each product.

I would like to go from.

Product 1
Acme
Product 2
Acme
Product 3
Acme

To this below and keep my UITableView\Cells

Acme
Product 1
Product 2
Product 3

….
Many Thanks for any help.

  • 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-06-12T20:25:32+00:00Added an answer on June 12, 2026 at 8:25 pm

    maybe it is not the fastest way, but i think it is simple

    first create a small inner class like this:

    @interface ProductSection
    @property (strong, nonatomic) NSString* sectionName;
    @property (strong, nonatomic) NSMutableArray* products;
    @end
    

    then use this instead your sort:

    NSSortDescriptor *aSort =[[NSSortDescriptor alloc] initWithKey:@"Dis" ascending:YES];
    NSArray* products = [distribArray sortUsingDescriptors:[NSMutableArray arrayWithObject:aSort]];
    
    self.sections = [NSMutableArray array];
    
    NSString* currentDistributor = nil;
    for (Product* p in products) {
        if (![p.Dis isEqualToString:currentDistributor]) {
            ProductSection* section = [[ProductSection alloc] init];
            section.sectionName = p.Dis;
            section.products = [NSMutableArray array];
            [self.sections addObject:section];
    
            currentDistributor = p.Dis;
        }
        ProductSection* section = [self.sections lastObject];
        [section.products addObject:p];
    
    }
    [self.tableView reloadData];
    

    where self.sections is a mutable array of ProductSection

    next use this in your Table View Data Source:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[[self.sections objectAtIndex:section] products] count];
    

    }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.sections count];
    

    }

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    [[self.sections objectAtIndex:section] sectionName];
    

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Product* p = [[[self.sections objectAtIndex:indexPath.section] products] objectAtIndex:indexPath.row];
    ...
    

    }

    hope that will help

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

Sidebar

Related Questions

I am sorting and sectioning a tableview using the first letter of an array
Using the below for sorting a multidimensional array by screen name: $sortArray = array();
I using jquery sorting ( http://jqueryui.com/demos/sortable/ ). Is it possible to sort (move list)
I am using JS sorting to call this method: def sort params[:piece].each_with_index do |id,
I'm sorting a NSMutableArray that has NSString that contain numbers. This is the code
I am sorting the UITableView by using a predicate with NSFetchedResultsController. But, when I
I'm doing sorting of a list of elements, using: jQuery.fn.sort = function() { return
Sorting five integers using bitwise or comparison operators can be achieved by first getting
Question is about sorting std::vector<myclass> using function sort from STL's algorithms class. Standard way
I'm seeing an issue where the NSFetchedResultsController is only sorting by the first NSSortDescriptor

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.