Alphabetical tableView separators

Discussion in 'iOS Programming' started by larswik, Dec 23, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    Can someone point me to the method or info so I can read up on the alphabetical tableView separators that you find in the contacts list? I have seen them in a couple other programs and looked at the methods in the tableview thinking it would be there, but nothing stood out.

    I first started to try to build something myself (code bellow) It was taking the first letter of a String at an index in my ClientArray, converting it to a Char and comparing the results. After tinkering for a hour I thought there had to be an easier way already made. The code is not done but this is what I was thinking. There must be a pre made solution for this? can I get any *'s in the right direction.

    Code:
            int letterNum = 0;
            int count = clientListForTable.count;
            char currentLetter, arrayLetter;
            
            NSArray *letters = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D", nil];
     
            for (int i = 0; i < count; i ++) {
                NSString *charLetter = [clientListForTable objectAtIndex:i];
                currentLetter = [charLetter characterAtIndex:0];
                
                NSString *getLetterFromArray = [letters objectAtIndex:letterNum];
                arrayLetter = [getLetterFromArray characterAtIndex:0];
                
                while (currentLetter != arrayLetter) { // Find first letter
                    letterNum++;
                    NSString *getLetterFromArray = [letters objectAtIndex:letterNum];
                    arrayLetter = [getLetterFromArray characterAtIndex:0];
                }
                
                [clientListForTable insertObject:[letters objectAtIndex:letterNum] atIndex:i];
    
                
            }
    
    Thanks
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Use Core Data...
    sort everything alphabetically...
    then run through the list grabbing the first letter of each name, and if the first letter of the current name matches the first letter of the prior name then it goes in the same section as the prior name, and if not, it starts a new section...

    That's how I would do it.
     
  3. forum user macrumors regular

    Joined:
    Aug 28, 2008
    #3
    there is
    Never used it myself though. There is a discussion in the System Guides under the heading "Populating an Indexed List"

    hth

    Olaf
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    Thanks guys! I have not used Core Data for any projects as of yet. This might be a good time to learn them. I will read up on the UILocalizedIndexedCollation. I already sort my array so it is in alphabetical order. Thought it would have been easier to do this but I will read up on it.

    Thanks.
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    Having an index on your tableview and using core data are orthogonal.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Do you mean the grey bars within the table that contain "A", "B", etc? Or do you mean the floating letters that run down the right side of the table?
     
  7. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    My original question was in regards to the grey bar above the names that just contained the A,B,C and so on. But if it is easy then the one on the right side might be a good addition too.

    I have not had time to read up on these yet and try them out. Family and holiday stuff get in the way of having fun learning this last week :)
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    The grey bars are what's known as section titles on a plain-style tableView. They are easily implemented using the UITableViewDataSource methods. As for the ones on the right side, those are called an indexed list. It might be a good idea to read through the entire Table View Programming Guide for iOS so you have a better idea of what all is available and possible with table views.
     
  9. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    Ahh, thanks! today I have some time to finally work on this stuff. I will start to read the docs this afternoon.

    Thanks again!
     

Share This Page