2011-03-26 31 views
0

我是一個UITableView設置並加載了大約3條記錄。 Interface Builder中UITableView的高度僅爲44像素。每行的高度設置爲44像素。爲什麼我的UITableView不能滾動到正確的索引行?

這意味着一次只能看到一行,但可以拖動並滾動到新行。我想UITableView也總是顯示滾動到最可見的行。

我已經安裝了下面的代碼:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { 
    // Get the center of the UITableView bounds  
    CGPoint tableViewCenter = self.activeTableView.center; 
    // Get the row that is currently occupying the center point in the UITableView 
    NSIndexPath *row = [self.activeTableView indexPathForRowAtPoint:CGPointMake(tableViewCenter.x, tableViewCenter.y)]; 
    // Animate and scroll to this row making it the only visible row. 
    [self.activeTableView scrollToRowAtIndexPath:row atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
} 

我的問題是,無論我做什麼,它總是滾動回於1行的記錄。我可以一直拖到第三排,然後放手,並一直滾動回到第一條記錄?如果在我放手之後它是最顯眼的行,它不應該滾動並居中第3行嗎?

謝謝!

回答

0

你的問題是,當你滾動tableView.center不會改變。只有當你改變tableview的框架時,中心纔會改變。

你應該使用的是UIScrollView的contentOffset。

與此更換你的tableViewCenter代碼,它應該工作

CGPoint tableViewCenter = scrollView.contentOffset; 
// contentOffset is the position of the first visible pixel in the upper left corner 
// add half the height so the relevant point is in the middle of the tableView 
tableViewCenter.y += self. activeTableView.frame.size.height/2; 
相關問題