UITableView索引在第14个字母后重复

时间:2012-10-21 16:35:26

标签: ios uitableview

我正在尝试制作一个带索引的字母表,当我点击前14个字母时它会显示正确的字母,但之后它会不断重复并仅向我显示第14个字母。请帮忙..这是我的代码:(这些字母不是英文:))

- (void)viewDidLoad
{
    [super viewDidLoad];

    _arr=[NSArray arrayWithObjects:@"א",@"ב",@"ג",@"ד",@"ה",@"ו",@"ז",@"ח",@"ט",@"י",@"כ",@"ל",@"מ",@"נ",@"ס",@"ע",@"פ",@"צ",@"ק",@"ר",@"ש",@"ת",nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_arr count];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.arr;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index {
    int position;
    switch (index) {
        case 0:
            position=0;
            break;
        case 1:
            position=1;
            break;
        case 2:
            position=2;
            break;
        case 3:
            position=3;
            break;
        case 4:
            position=4;
            break;
        case 5:
            position=5;
            break;
        case 6:
            position=6;
            break;
        case 7:
            position=7;
            break;
        case 8:
            position=8;
            break;
        case 9:
            position=9;
            break;
        case 10:
            position=10;
            break;
        case 11:
            position=11;
            break;
        case 12:
            position=12;
            break;
        case 13:
            position=13;
            break;
        case 14:
            position=14;
            break;
        case 15:
            position=15;
            break;
        case 16:
            position=16;
            break;
        case 17:
            position=17;
            break;
        case 18:
            position=18;
            break;
        case 19:
            position=19;
            break;
        case 20:
            position=20;
            break;
        case 21:
            position=21;
            break;
        case 22:
            position=22;
            break;


        default:
            position=1;
            break;
    }
    NSLog(@"scrolling to %d",position);
    [tableView reloadInputViews];
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:position inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

     return index;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.arr objectAtIndex:section];
}

- (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];
    }

       cell.textLabel.font = [UIFont boldSystemFontOfSize:32];
       cell.textLabel.text=[_arr objectAtIndex:indexPath.row];
    return cell;
}

1 个答案:

答案 0 :(得分:0)

此处的所有内容都是正确的,除了tableView:sectionForSectionIndexTitle:atIndex

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index {
    // First of all, we can greatly simplify it by removing your case statement
    // and replacing it with this:
    int position = 1;

    if (index < 23) {  // Or even better, use [_arr count]
        position = index;
    }

    NSLog(@"scrolling to %d",position);

    // Then, the following three lines should be deleted, as the tableview
    // automatically scrolls for you when an index is tapped:

    // [tableView reloadInputViews];
    // [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    // [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:position inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

    // Also, you should be returning position, and not index
    return position;
}

当您使用索引时,tableview会自动处理滚动,因此您不应该自己动手。只需删除此例程中的所有[tableView ...]方法,它应该适合您。

所以现在,短版本将是:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index {
    if (index < [_arr count]) {
        return index;
    }

    // Default
    return 1;
}