Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make UITableView scrollable by index

I am trying to create a UITableView index for faster scrolling of a large UITableView, I have the index showing up on the right but when I try to scroll with it, it does not work..

screenshot

I am wondering if I have missed something so I am wondering if there is something else I have to do, or something along those lines.

#pragma - TableView Index Scrolling

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

    if(searching)
        return nil;

    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    [tempArray addObject:@"A"];
    [tempArray addObject:@"B"];
    [tempArray addObject:@"C"];
    [tempArray addObject:@"D"];
    [tempArray addObject:@"E"];
    [tempArray addObject:@"F"];
    [tempArray addObject:@"G"];
    [tempArray addObject:@"H"];
    [tempArray addObject:@"I"];
    [tempArray addObject:@"J"];
    [tempArray addObject:@"K"];
    [tempArray addObject:@"L"];
    [tempArray addObject:@"M"];
    [tempArray addObject:@"N"];
    [tempArray addObject:@"O"];
    [tempArray addObject:@"P"];
    [tempArray addObject:@"Q"];
    [tempArray addObject:@"R"];
    [tempArray addObject:@"S"];
    [tempArray addObject:@"T"];
    [tempArray addObject:@"U"];
    [tempArray addObject:@"V"];
    [tempArray addObject:@"W"];
    [tempArray addObject:@"Y"];
    [tempArray addObject:@"X"];
    [tempArray addObject:@"Z"];

    return tempArray;
}

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

    if(searching)
        return -1;

    return index % 2;
}
like image 658
C.Johns Avatar asked Sep 13 '11 21:09

C.Johns


3 Answers

You can use this

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

Which tells the table which section corresponds to section title/index (e.g. "B",1))

This method is for returning a section corresponding to a Title. But you want to scroll your single section's row according to these titles. So follow these steps:

  1. Create global array means declare it in .h file

    NSMutableArray *alphabetsArray;
    NSMutableArray *dataArray; //--This contain your cell value i.e. Honda,Mazda--//
    
  2. Define it in viewDidLoad method like below then return it from UITableView method..

    alphabetsArray = [[NSMutableArray alloc] init];
    [alphabetsArray addObject:@"A"];
    [alphabetsArray addObject:@"B"];
    [alphabetsArray addObject:@"C"];
    [alphabetsArray addObject:@"D"];
    [alphabetsArray addObject:@"E"];
    [alphabetsArray addObject:@"F"];
    [alphabetsArray addObject:@"G"];
    [alphabetsArray addObject:@"H"];
    [alphabetsArray addObject:@"I"];
    [alphabetsArray addObject:@"J"];
    [alphabetsArray addObject:@"K"];
    [alphabetsArray addObject:@"L"];
    [alphabetsArray addObject:@"M"];
    [alphabetsArray addObject:@"N"];
    [alphabetsArray addObject:@"O"];
    [alphabetsArray addObject:@"P"];
    [alphabetsArray addObject:@"Q"];
    [alphabetsArray addObject:@"R"];
    [alphabetsArray addObject:@"S"];
    [alphabetsArray addObject:@"T"];
    [alphabetsArray addObject:@"U"];
    [alphabetsArray addObject:@"V"];
    [alphabetsArray addObject:@"W"];
    [alphabetsArray addObject:@"Y"];
    [alphabetsArray addObject:@"X"];
    [alphabetsArray addObject:@"Z"];
    

    [Note: You can also create alphabetsArray with dynamic contains according to your dataArray. For this follow How we can create dynamic section index titles array at below]

  3. Now use the above mentioned method to get the title

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        return alphabetsArray;
    }
    
  4. Write your logic to scroll the UITableView

    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
        for (int i = 0; i< [dataArray count]; i++) {
            // Here you return the name i.e. Honda,Mazda 
            // and match the title for first letter of name
            // and move to that row corresponding to that indexpath as below
            NSString *letterString = [[dataArray objectAtIndex:i] substringToIndex:1];
            if ([letterString isEqualToString:title]) {
                [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
                break;
            }
        }
        return -1;
    }
    

****How we can create dynamic section index titles array****

  1. Let us suppose that dataArray is like that

    [@"Honda",@"Maruti",@"Mazda",@"Toyota",.....]  
    
  2. Create a method for creating dynamic section index titles array as below

    - (void)createAlphabetArray;
    
  3. Implements createAlphabetArray method as below

    #pragma mark - Create Alphabet Array
    - (void)createAlphabetArray {
        NSMutableArray *tempFirstLetterArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < [dataArray count]; i++) {
            NSString *letterString = [[dataArray objectAtIndex:i] substringToIndex:1];
            if (![tempFirstLetterArray containsObject:letterString]) {
                [tempFirstLetterArray addObject:letterString];
            }
        }
        alphabetsArray = tempFirstLetterArray;
        [tempFirstLetterArray release];
    }
    
  4. Now just call it anywhere when you want to change alphabetsArray contains

    [self createAlphabetArray]; 
    

Now you can create dynamic section index titles array dynamically according to your dataArray contents.

like image 69
Lalit Kumar Maurya Avatar answered Nov 03 '22 00:11

Lalit Kumar Maurya


The table view needs to be grouped alphabetically I believe - The temp array doesn't know how to translate the letter that you select into a specific index (it doesn't know what "a", "b", or "c" mean).

If you take a look at the ipod song list, you'll notice that each letter group has a letter section divider. You'll need to group your table objects into sections titled with letters in order to translate the letter "A" to an index. What apple's ipod list is actually doing is not scrolling to the first "A" entry, but the beginning of the "A" section.

like image 35
Daniel G. Wilson Avatar answered Nov 03 '22 01:11

Daniel G. Wilson


You might try:

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

It's hard to tell what's wrong without the rest of your controller, but I think that should work.

like image 21
Evan Cordell Avatar answered Nov 02 '22 23:11

Evan Cordell