Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

iphone - 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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...