Question

So I have a list.plist with all my items in it. They are all strings with a Key and a Value.

I have 'courses' declared as a NSDictionary and 'courseKeys' declared as NSArray.

To load all the items into my table-view I used this code

My question is, how can I make it so that each starting letter has its own section, so I can easily navigate between letters?

I know that I can set the sectionIndexTitlesForTableView and I can show letters A-Z on the right sidebar, but how do I make them navigate to the correct letter?

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSMutableArray *searchArray = [NSMutableArray arrayWithObjects:
                        @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L",
                        @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];


return searchArray;
}
Was it helpful?

Solution

You should implement 3 table view delegates to make it work. I will try to write a semi pseudo-code for you, to make it simple:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [searchArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *courses = [self coursesInSection:section];
    return [courses count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

       NSArray *courses = [self coursesInSection:indexPath.section];
       NSString *courseName = [courses objectAtIndex:indexPath.row];

       cell.textLabel.text = courseName;
       return cell;

}

All you need to do is implement - (NSArray *)coursesInSection:(NSInteger)section according to needed logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top