0

I have a UITableView that is using alphabetical scroll but when I tab on a specific letter it is not doing . As sample if i tab on "G" its not jumping to "G" it is just scrolling 26 letters down why so ever .

My Code

 alphabetical =[NSArray 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];


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

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

    NSInteger newRow = [self indexForFirstChar:title inArray:alphabetical];
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:newRow inSection:0];
    [tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

    return index;
}

- (NSInteger)indexForFirstChar:(NSString *)character inArray:(NSArray *)array
{
    NSUInteger count = 0;
    for (NSString *str in array) {
        if ([str hasPrefix:character]) {
            return count;
        }
        count++;
    }
    return 0;
}

I think something with my indexForFirstChar is wrong when I add there a breakpoint it is just going throw all letters and its not jumping to one specific ..

I have no sections in this table view

Thanks for help and fast answer !!

4

2 回答 2

0

无需滚动到sectionForSectionIndexTitle. 刚回来index。请参阅文档

另外,这里给你一个小技巧:

alphabetical = [@"ABCDEFGHIJKLMNOPQRSTUVWXYZ" componentsSeparatedByString:@""]; 
于 2013-07-16T15:12:32.233 回答
0

我自己得到了解决方案,它非常简单,或多或少不需要评论它是如何工作的

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    if (alphabetical)
    {
        NSMutableArray *charactersForSort = [[NSMutableArray alloc] init];
        for (NSDictionary *item in employees)
        {
            if (![charactersForSort containsObject:[[item valueForKey:@"LastName"] substringToIndex:1]])
            {
                [charactersForSort addObject:[[item valueForKey:@"LastName"] substringToIndex:1]];
            }
        }
        return charactersForSort;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    BOOL found = NO;
    NSInteger b = 0;
    for (NSDictionary *item in employees)
    {
        if ([[[item valueForKey:@"LastName"] substringToIndex:1] isEqualToString:title])
            if (!found)
            {
                [_tblPersonDetail scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:b inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
                found = YES;
                break;
            }
        b++;
    }
    return b;
}

如果您仍有问题,请随时提问

于 2013-07-17T12:54:57.557 回答