从 UITableView 获取 "Pull to Refresh" 的最佳方式是什么?

What's the Best Way to Get a "Pull to Refresh" from a UITableView?

我有一个 UITableView,它显示从连接 WiFi 的设备接收到的数据。

我想执行相当标准的手势响应,即向下拖动 table,然后松开,以触发更新。

我想我可以将滑动手势识别器附加到 table 的底层视图,但我想知道是否有办法直接从 table 本身拦截此手势。

META:

I've been looking through the various docs and whatnot. I haven't had to do this before, so I never gave it any thought. Now I see that I probably should have thought about it.

If this is an obvious solution, I don't mind being told so, as long as you help me to find the "obvious" remedy. I'll delete this question if it's a bad one.

However, I haven't found a solution, and I'm usually fairly good at that kind of thing.

UPDATE I changed the title to better reflect what I'm asking.

如果您将 UIScrollViewDelegate 添加到您的 UITableView,您可以响应 scrollViewDidScroll: 事件。负值 contentOffset.y 表示您的用户已在您的表格视图顶部下拉。

完全原生的方法是在 UIViewController 中使用 UITableViewController,例如

{
    tableViewController = [[UITableViewController alloc] init];
    [tableViewController setTableView:myTableView];

    refreshControl = [[UIRefreshControl alloc] init];
    [tableViewController setRefreshControl:refreshControl];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
}

这就是我最终所做的。尽管这不是最 "legal" 的做事方式,但它仍然非常有效,而且非常简单:

我像这样设置了一个滚动视图委托陷阱,并将其添加到委托中class:

//*******************************************
// Reacts to the table's scroller being changed.
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                     withVelocity:(CGPoint)velocity
              targetContentOffset:(inout CGPoint *)targetContentOffset {
#ifdef DEBUG
    NSLog ( @"MyViewController::scrollViewWillEndDragging:%@withVelocity:(%f,%f)targetContentOffset:(%f,%f)",scrollView,velocity.x, velocity.y, targetContentOffset->x, targetContentOffset->y );
#endif

    // We have to have had a "hard pull South" to trigger a refresh of the objects.
    if ( (velocity.y < 0) && ([scrollView contentOffset].y < -60) ) {
#ifdef DEBUG
        NSLog ( @"We will perform a new refresh." );
#endif
    }
}

“-60”是因为我在将其设置为静态之前正在寻找最佳偏移量。