开发者

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..

Make UITableView scrollable by index

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];
    [tem开发者_如何学CpArray 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;
}


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜