Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Table View organization into Alphabetical sections

greeneye82greeneye82 Posts: 21Registered Users
edited June 2012 in iOS SDK Development
ok, onto my next dilemma. I've done a ton of Googling and it seems there are several ways to go about it, but none seem to fit my structuring of my table view. I'm using Xcode 4.3.2 with ARC and storyboards. I have a plain view table view. My table consists of a list of cities and their departments (one city for each row), when the user selects a city they will be pushed to a detail view of about that city. I want to organize my table alphabetically, with a section header. Any recommendations, hints, links to tutorials, would be greatly appreciated.

I have a NSMutable array set up like this in my table view .m
NSMutableArray *dept;   


- (void)viewDidLoad

{
    [super viewDidLoad];

    dept = [[NSMutableArray alloc] init];
    
    City *thedept = [[City alloc] init];
    [thedept setCityname:@"Albany"];
    [thedept setPhone:@"5551237890"];
    [dept addObject:thedept];
    
    thedept = [[City alloc] init];
    [thedept setCityname:@"Boston"];
    [thedept setPhone:@"5556543435"];
    [dept addObject:thedept];
    
    thedept = [[City alloc] init];
    [thedept setCityname:@"Springfield"];
    [thedept setPhone:@"5553456323"];
    [dept addObject:thedept];
    
    thedept = [[City alloc] init];
    [thedept setCityname:@"Tallahassee"];
    [thedept setPhone:@"5552450988"];
    [dept addObject:thedept];

    thedept = [[City alloc] init];
    [thedept setCityname:@"Vacouver"];
    [thedept setPhone:@"5551312555"];
    [dept addObject:thedept];

The section header can just be the letter of the alphabet (A, B, C, D...). I'm thinking I need to pre-organize my cities by their first letter and maybe name the section based on the letter (such as "sectionAcities"). The cities I have given are just a few, I have about 100 total.
Post edited by greeneye82 on
· ·

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited May 2012
    Your search term is: UILocalizedIndexedCollation

    You might also get some use out of this: Clinging To Ideas: Taming Table Views
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • greeneye82greeneye82 Posts: 21Registered Users
    edited May 2012
    Brian, thanks for that link on Clinging To Ideas. It looks like it is just what I need. However, when I tried it out it doesn't seem to work. Maybe it's because I am using xcode 4.3.2 with storyboards and ARC.
    I'm still pretty new at this, but how would I structure the
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	
    NSMutableArray *keys = [[NSMutableArray alloc] init];
    NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
    	
    NSString *aKey = @"A";;
    NSString *bKey = @"B";;
    NSString *cKey = @"C";;
    	
    	
    [contents setObject:[NSArray arrayWithObjects:@"Andover", @"Ansonia";, nil] forKey:aKey];
    	
    [contents setObject:[NSArray arrayWithObjects:@"Berlin", @"Bridgeport";, @"Bristol";, nil] forKey:bKey];
    	
    [contents setObject:[NSArray arrayWithObjects:@"Canaan", @"Cheshire";, @"Chester";, @"Colchester";, 
        @"Cromwell";, nil] forKey:cKey];
        
    
    [keys addObject:aKey];	
    [keys addObject:bKey];
    [keys addObject:cKey];
    	
    [self setSectionKeys:keys];
    [self setSectionContents:contents];
    
    part of your code (I added in some of the data I need) to also include a phone number for each respective city, then be able to pass it to the next scene?
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited May 2012
    Pro tip: "It doesn't work" is probably the least useful phrase in programming. You need to be specific about what was expected, and how this compares to what actually happened. It also helps if you post code demonstrating what you have done.

    Now then, I don't understand what you are asking for, as you seem to have a fine start there.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • greeneye82greeneye82 Posts: 21Registered Users
    edited May 2012
    Let me try this instead. Take a look at this code here for my table view controller implementation file. My goal is to have the appropriate alphabet letter (the first letter of the city, for instance Albany would be in section A.) in a section header for my plain table view. This is just generic, the websites here are fake. When the user selects the row, it will bring them to the next screen where there will be a button linking them to the appropriate website. My real app will have phone numbers not websites, but obviously I can't test phone numbers in my simulator.
    Presently, my app works the way I want it to, I just want to add in section headers, so I am probably going to have to restructure my data.
    My question is, how can I incorporate your coding assistance, like I saw in your Clinging to Ideas blog into my already assembled data structure?
    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        
        if ([segue.identifier isEqualToString:@"ShowInfo"]) {
        InfoViewController *ivc = [segue destinationViewController];
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        int row = [path row];
        City *c = [dept objectAtIndex:row];
        ivc.currentCity=c;
        }   
    
        
    }
        
        
    - (id)initWithStyle:(UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
                // Custom initialization
            }
        return self;
    }
        
    
    NSMutableArray *dept;   
    
    
    - (void)viewDidLoad
    
    {
        [super viewDidLoad];
    
        dept = [[NSMutableArray alloc] init];
        
        City *thedept = [[City alloc] init];
        [thedept setCityname:@"Akron"];
        [thedept setPhone:@"www.youtube.com"];
        [dept addObject:thedept];
        
        thedept = [[City alloc] init];
        [thedept setCityname:@"Albany"];
        [thedept setPhone:@"www.yahoo.com"];
        [dept addObject:thedept];
        
        thedept = [[City alloc] init];
        [thedept setCityname:@"Bridgeport"];
        [thedept setPhone:@"www.yahoo.com"];
        [dept addObject:thedept];
        
        thedept = [[City alloc] init];
        [thedept setCityname:@"Portland"];
        [thedept setPhone:@"www.facebook.com"];
        [dept addObject:thedept];
    
        thedept = [[City alloc] init];
        [thedept setCityname:@"Richmond"];
        [thedept setPhone:@"www.myspace.com"];
        [dept addObject:thedept];
    
        thedept = [[City alloc] init];
        [thedept setCityname:@"Springfield"];
        [thedept setPhone:@"www.google.com"];
        [dept addObject:thedept];
    
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited May 2012
    What part do you not understand?
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • greeneye82greeneye82 Posts: 21Registered Users
    edited May 2012
    How to set up the array for the A to Z sections
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited May 2012
    You are already doing it, at least up to C. So keep going.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • greeneye82greeneye82 Posts: 21Registered Users
    edited May 2012
    Could you show me an example of what my code should look like if I have the extra data, to pass over the website/phone number? I understand how to just display the alphabetized city names from your example, but I want to be able to pass the other info associated with that city.
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited May 2012
    See the model object link in my signature.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • greeneye82greeneye82 Posts: 21Registered Users
    edited May 2012
    Your model-object post is pretty cool and easy to follow. Except i am still not grasping how to incorporate my phone numbers/website into the data set to be displayed on the next view once a row is selected. Basically, I don't understand where I should be putting the phone number/website data in the array. Can you show me an example of how that would be arranged?
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	
    	NSMutableArray *keys = [[NSMutableArray alloc] init];
    	NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
    	
        //these strings here are the section labels
    	NSString *aKey = @"A";;
    	NSString *bKey = @"B";;
    	NSString *cKey = @"C";;
    	NSString *dKey = @"D";;
    	
    [contents setObject:[NSArray arrayWithObjects:@"Andover", @"Ansonia";, nil] forKey:aKey];
    [contents setObject:[NSArray arrayWithObjects:@"Berlin", @"Bridgeport";, @"Bristol";, nil] forKey:bKey];
    [contents setObject:[NSArray arrayWithObjects:@"Canaan", @"Cheshire";, @"Chester";, @"Colchester";, 
         @"Cromwell";, nil] forKey:cKey];
    [contents setObject:[NSArray arrayWithObjects:@"Danbury", @"Darien";, @"Deep River", @"Derby";, 
         @"Durham";, nil] forKey:dKey];
        
        
        [keys addObject:aKey];	
        [keys addObject:bKey];
        [keys addObject:cKey];
        [keys addObject:dKey];
    	
    	
    	[self setSectionKeys:keys];
    	[self setSectionContents:contents];
    	
    	[keys release], keys = nil;
    	[contents release], contents = nil;
    	
    	
    }
    
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited May 2012
    I don't see where you are even making an attempt to incorporate the items that I have described. Make an effort. I'm not going to write this code for you.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • greeneye82greeneye82 Posts: 21Registered Users
    edited May 2012
    Still struggling here but I am seeing some progress. I've adapted some code that you recommended in your examples and I have gotten where I am now. This format will display and push the appropriate data, and display the section headers I want. However, it is not placing the appropriate city within the correct alphabetical section in my table. I am looking to find out how to do that. What I am thinking is that i need to place the
    forKey:aKey];
    forKey:bKey];
    forKey:cKey];
    
    somewhere. I tried to place it at the end of my [dept addObject:thedept]; line and the Cityname line, but those gave a error--Bad receiver type void.

    Here is my code...
    NSMutableArray *dept;   
    
    
    - (void)viewDidLoad
    
    {
        [super viewDidLoad];
        
        NSMutableArray *keys = [[NSMutableArray alloc] init];
        
        NSString *aKey = @"A";;
        NSString *bKey = @"B";;
        NSString *cKey = @"C";;
        
        dept = [[NSMutableArray alloc] init];
        
        City *thedept = [[City alloc] init];
        [thedept setCityname:@"Andover"];   
        [thedept setPhone:@"www.youtube.com"];
        [dept addObject:thedept];
        
        thedept = [[City alloc] init];
        [thedept setCityname:@"Ansonia"];
        [thedept setPhone:@"www.google.com"];
        [dept addObject:thedept];
        
        thedept = [[City alloc] init];
        [thedept setCityname:@"Berlin"];
        [thedept setPhone:@"www.yahoo.com"];
        [dept addObject:thedept];
        
        thedept = [[City alloc] init];
        [thedept setCityname:@"Bridgeport"];
        [thedept setPhone:@"www.facebook.com"];
        [dept addObject:thedept];
    
        thedept = [[City alloc] init];
        [thedept setCityname:@"Bristol"];
        [thedept setPhone:@"www.twitter.com"];
        [dept addObject:thedept];
    
        thedept = [[City alloc] init];
        [thedept setCityname:@"Canaan"];
        [thedept setPhone:@"www.myspace.com"];
        [dept addObject:thedept];
    
        thedept = [[City alloc] init];
        [thedept setCityname:@"Cheshire"];
        [thedept setPhone:@"www.msn.com"];
        [dept addObject:thedept];
    
        thedept = [[City alloc] init];
        [thedept setCityname:@"Chester"];
        [thedept setPhone:@"www.nbc.com"];
        [dept addObject:thedept];
    
        thedept = [[City alloc] init];
        [thedept setCityname:@"Colchester"];
        [thedept setPhone:@"www.cbs.com"];
        [dept addObject:thedept];
    
        thedept = [[City alloc] init];
        [thedept setCityname:@"Cromwell"];
        [thedept setPhone:@"www.abc.com"];
        [dept addObject:thedept];
    
        [keys addObject:aKey];
        [keys addObject:bKey];
        [keys addObject:cKey];
    	
        [self setSectionKeys:keys];    
    }
    
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited May 2012
    There are links to 2 resources in my signature, and I have provided you with a 3rd. As far as I can tell, you are only using what was mentioned in one of them. So, reread the others, multiple times if necessary.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • greeneye82greeneye82 Posts: 21Registered Users
    edited June 2012
    I believe I AM using your model object example to set up my data structure. I have successfully been able to complete all your training tutorials on Table Views as well. Now, I am just trying to incorporate the 'forKey:aKey'' part to link up the alphabetic letter to my row data (city name). But it seems no matter where I place it, I am getting an error stating "use of undeclared identifier 'forKey'"

    My question now is, if I have my data set up as a model-object how do or where do I describe which section key each set goes into?
    City *thedept = [[City alloc] init];
        [thedept setCityname:@"Albany"];
        [thedept setPhone:@"www.youtube.com"];
        [dept addObject:thedept];
            
        thedept = [[City alloc] init];
        [thedept setCityname:@"Albauquerque"];
        [thedept setPhone:@"www.google.com"];
        [dept addObject:thedept];
        [forKey:aKey];                                                 <--error
        
        thedept = [[City alloc] init];
        [thedept setCityname:@"Boston"];
        [thedept setPhone:@"www.yahoo.com"];
        [dept addObject:thedept]
    
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited June 2012
    BrianSlick wrote: »
    Your search term is: UILocalizedIndexedCollation

    You might also get some use out of this: Clinging To Ideas: Taming Table Views

    This is the post that you seem to be overlooking.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • greeneye82greeneye82 Posts: 21Registered Users
    edited June 2012
    Yeah that is way over my head. Did I mention this is my first project?
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited June 2012
    Ok, then you're not ready for this project yet, and should be spending your time with a good iOS development book.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
Sign In or Register to comment.