2012-10-06 71 views
1

我想索引表並滾動到正確的行時單擊索引條目。該表的每一行中都有字母A..Z。該指數也有A..Z。它在滾動時運作。例如,當我點擊索引中的'H'時,表格會滾動以便H是第一行,但現在當我點擊'A'時,屏幕上有一半的空白,然後中間出現一行。這是爲什麼?由於我是新成員,因此我無法發佈圖片。UITableView索引滾動問題

我只有一個部分。我爲部分計數返回1,部分計數爲26。

我在方法的代碼sectionForSectionIndexTitle是

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

}

+0

您是否嘗試過爲每個字母而不是僅爲1創建一個部分? – Hackmodford

+1

試試看 - [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:position inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES]; – 2012-10-06 04:20:26

+0

我正在嘗試不同的參數設置,當我嘗試動畫:沒有它工作! [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:position inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; – venkatmv

回答

3

試試這個東西..... 我這是工作..

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

是的!看起來像動畫:NO是需要定位的。請看我上面的評論。謝謝。 – venkatmv

0

K作爲從什麼的問題

  • 看一看的UITableView數據源和代表
  • 正確設置表格

Indexpath是一種有兩個參數行和部分

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

在你 didSelectRowAtIndexPath方法方法,你將有indexpath與你點擊 因此

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

將足以實現時要滾動這

+0

索引實際上是一個NSIndexPath。順便說一句,我試着動畫:不喜歡這樣,它的工作。 [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:position inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];它沒有使用動畫:YES – venkatmv