Jack Nutkins
Jack Nutkins

Reputation: 1555

Displaying Elements in UITableView like iPhone contacts, separated into alphabetical sections?

I've been looking around on-line for a solution to this, but I can't seem to find anything, and I'm stuck.

I have a NSMutableArray:

productsToDisplay

Which consists of a heap of Product objects that each have a 'name' attribute e.g.

Product *product = ....

NSLog(@"%@", product.name);

Each cell displays a products name and when clicked, displays more information about the product.

However I have a lot of products and would like to split them up into alphabetical sections in the UITableView (like the Contacts on your iPhone are).

Can someone point me in the right direction?

Thanks a lot,

Jack

[EDIT] I realise I could just sort the array on the name attribute, but I would also like the quick search bar that runs down the right hand side of the UITableView so a user can easily find the section they are looking for.

Upvotes: 2

Views: 3193

Answers (2)

Shefali Soni
Shefali Soni

Reputation: 1302

Add alphabetic sections to ur tableview, then You have to create array of alphabets corresponding to the section in tableview. Like section[0] - 'A', section[1] - 'B', etc.

Add - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return keys; } //the delegate is asking for an array of the values to display in the index.

So the first item in this array will take the user to the first section, which is section 0.

Upvotes: 3

Wasif Saood
Wasif Saood

Reputation: 2008

use this delegate to display the right hand sections list:-

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return searchArray; 
}

use this delegate to associate table content (section) according to right hand list:-

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    if (index == 0) {
    [tableView scrollRectToVisible:[[tableView tableHeaderView] bounds] animated:NO];
        return -1;
    }
    return index;
}

Upvotes: 4

Related Questions