Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

boyplunder

macrumors regular
Original poster
Sep 8, 2008
165
0
UK
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.]
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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?
 

boyplunder

macrumors regular
Original poster
Sep 8, 2008
165
0
UK
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.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.