简体   繁体   中英

UITableView Index scrolling issue

I am trying to index a table and scroll to the correct row when an index entry is clicked. The table has the letters A..Z one in each row. The index also has A..Z. It works while scrolling down. For example when I click on 'H' in the index the table scrolls so that H is the first row but now when I click on 'A' there is blank space for half the screen on then A row shows up in the middle. Why is that? Since I am a new member I am not able to post pictures.

I have only one section. I return 1 for section count and 26 for section rows count.

The code I have in the method sectionForSectionIndexTitle is

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
NSLog(@"getting called %d",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;
    case 23:
        position=23;
        break;    
    case 24:
        position=24;
        break;
    case 25:
        position=25;
        break;
    case 26:
        position=26;
        break;
    default:
        position=1;
        break;
}
NSLog(@"scrolling to %d",position);
[tableView reloadInputViews];
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:position inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
return index;

}

Try this thing..... I this is working..

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

K as of what from the question

  • Have a look at the UITableView datasources and delegates
  • set up table properly

Indexpath is having two parameters rows and sections

NSIndexPath *index=[NSIndexPath indexPathForRow:position inSection:section]
[tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES];

In your didSelectRowAtIndexpath method you will have the indexpath with you and to scroll when clicked and hence

[tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionTop animated:YES];

will be enough to achieve this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM