【问题标题】:Why is my UITableView not scrolling to the correct index row?为什么我的 UITableView 没有滚动到正确的索引行?
【发布时间】:2011-03-26 23:39:06
【问题描述】:

我设置了一个 UITableView 并加载了大约 3 条记录。 Interface Builder 中 UITableView 的高度只有 44 像素。每行的高度设置为 44 像素。

这意味着一次只能看到一行,但可以拖动并滚动到新行。我希望 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];
}

我的问题是,无论我做什么,它总是滚动回第一行记录。我可以一直拖到第 3 行然后放手,它会一直滚动到第 1 条记录?如果我放手后第三行是最明显的行,它不应该滚动并居中吗?

谢谢!

【问题讨论】:

    标签: iphone cocoa-touch uitableview ios4 uiscrollview


    【解决方案1】:

    您的问题是滚动时 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 2021-04-29
      相关资源
      最近更新 更多