Client list for Tableview

Discussion in 'iOS Programming' started by larswik, Sep 25, 2012.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    I'm creating a client list that will be sorted alphabetically in a UITableView with 26 sections (alphabetically A-Z). I have an NSDictionary with the clients and the keys are the client names that I want to use for the Array for the tableView.

    I started to write a custom method to pull the first character of each key in the Client Dictionary and then try to sort it in to 1 of 26 arrays A,B,C and so on. At that point I thought there must be some class already created to simplify this process but what is it called, or is there none?
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Break it down:
    1. Visit each key in the dictionary.
    2. Pull the first character of each key.
    3. Make a string from that character.
    4. Put the strings in an array.
    5. Sort the array.

    For #1, look at NSDictionary's reference doc for methods that return all keys, or enumerate keys, or otherwise give access to keys (plenty of keywords in there).

    For #2 & #3, look at NSString's reference doc for methods that access a specific character at index, or make a new string from a sub-range.

    For #4, look at NSMutableArray's reference doc for a method that appends an object.

    For #5, look at NSArray and NSMutableArray for a method that sorts objects in the array.

    How many lines of code do you think all the above will take? Why would there need to be a class that only did that, when you can build it up yourself from simpler pieces? You should look at classes as building-blocks that require some assembly, rather than as pre-fabricated solutions you plug in and turn on.

    If you think your code is too complex because you're overlooking a simpler way to do it, you'll have to post that code.
     
  3. TheWatchfulOne macrumors 6502

    TheWatchfulOne

    Joined:
    Jun 19, 2009
    #3
    larswik, are you trying to implement an index for your table view?

    If so, you might be interested in the UILocalizedIndexCollation class.

    I've used it myself and I find it works very well. :cool:
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    Chown33, what you posted was almost exactly what I had outlined. Except after number 4. I had created 1 array for each letter so all the clients that started with a C would be added to the C array. After that I would add all the arrays to an NSDictionary with a Key of 'A' which would store and array object with all the clients that start with the letter 'A'.

    In the TableView delegate for the number of rows in section, if section was equal to 0 then I would pull the array 'A' from the dict and return the array count.

    I knew that, I knew enough about the logic flow that I would pull it off. But there have been a number of times where I start to write out the code only to find out that some Method in some class does the same thing that I am trying to accomplish already. At that point I google it || ask this forum.

    TheWatchfulOne - That is what I am trying to do. I will read up on that class now and see if that is what I was hoping was out there already.

    Thanks guys!
     

Share This Page