Tableview index problem

Discussion in 'iOS Programming' started by boyplunder, Oct 22, 2008.

  1. boyplunder macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #1
    Hi All,

    I am trying to get to grips with adding an index to a tableview, just like the one in contacts. I am confident in setting up the tableview, controls and UI settings, and generally all is going well. Except I can't figure out what I'm missing here. The app builds fine, with no errors, and it appears fine in the iPhone simulator. It just doesn't have the index down the side.

    This is the code I'm using. This is in the delegate.m file, which I think is the right place for it. 'entries' is the NSMutableArray I have of the data list.

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    return [entries valueForKey:mad:"letter"];
    }

    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

    return [entries indexOfObject:title];
    }​

    Any comments or advice would be most appreciated. If anyone has a really simple example I can review, that would be wonderful.
    [I have been using the simple index example from the Apple site, but can't see the wood for the trees.]
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    sectionForSectionIndexTitle: isn't required and I would comment it out for now.

    Is your sectionIndexTitlesForTableView: method being called? If so what is in the array you are returning?
     
  3. boyplunder thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #3
    Index call

    Thanks PhoneyDeveloper,

    Quite right about the first line, wasn't paying attention there. I do intend to have section titles, but one thing at a time!

    Having gone back to it on the strength of your comment, calling it is probably the bit that looks vague to me. The 'SimpleIndexTableView' example is difficult to track for a newbie, as it melds a lot of other things before you get to the addition of the index.

    I've been through it quite a few times to chart the code added from the previous example file and how it works, but just haven't been able to crack it. Could you give me a pointer as to what the calling code would be and where it should go. It's important to me that I learn this properly, as it's a big part of the apps I want to work on.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    The SimpleIndexedTableView is a good example. If you comment out the sectionForSectionIndexTitle: in that example you'll see that it still works exactly the same. The reason is that the section titles are letters of the alphabet, which match the letters in the index.

    The key to this is the two methods: titleForHeaderInSection and sectionIndexTitlesForTableView. The first just returns the title for each section, A, B, C... and the second returns the array of indexes, also A, B, C...
     

Share This Page