1

我正在尝试制作一个带有索引的字母表,当我单击前 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;
}
4

1 回答 1

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;
}
于 2012-10-21T20:08:55.713 回答