没有UITableViewController的UIRefreshControl

    技术2025-04-15  12

    本文翻译自:UIRefreshControl without UITableViewController

    Just curious, as it doesn't immediately seem possible, but is there a sneaky way to leverage the new iOS 6 UIRefreshControl class without using a UITableViewController subclass? 只是好奇,因为它似乎不可能,但有没有一个偷偷摸摸的方式利用新的iOS 6 UIRefreshControl类而不使用UITableViewController子类?

    I often use a UIViewController with a UITableView subview and conform to UITableViewDataSource and UITableViewDelegate rather than using a UITableViewController outright. 我经常使用带有UITableView子视图的UIViewController ,并且符合UITableViewDataSource和UITableViewDelegate而不是直接使用UITableViewController 。


    #1楼

    参考:https://stackoom.com/question/qRHg/没有UITableViewController的UIRefreshControl


    #2楼

    Well UIRefreshControl is a UIView subclass, so you can use it on it's own. 好的UIRefreshControl是一个UIView子类,所以你可以自己使用它。 I'm not sure though how it renders itself. 我不确定它是如何渲染自己的。 The rendering could simply depend on the frame, but it also could rely on a UIScrollView or the UITableViewController. 渲染可以简单地依赖于框架,但它也可以依赖于UIScrollView或UITableViewController。

    Either way, it's going to be more of a hack than an elegant solution. 无论哪种方式,它都将是一个黑客而不是一个优雅的解决方案。 I recommend you look into one of the available 3rd party clones or write your own. 我建议您查看一个可用的第三方克隆或编写自己的克隆。

    ODRefreshControl ODRefreshControl

    SlimeRefresh SlimeRefresh


    #3楼

    On a hunch, and based on DrummerB's inspiration, I tried simply adding a UIRefreshControl instance as a subview to my UITableView . 在预感中,基于DrummerB的灵感,我尝试将UIRefreshControl实例作为子视图添加到我的UITableView 。 And it magically just works! 它神奇地起作用!

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged]; [self.myTableView addSubview:refreshControl];

    This adds a UIRefreshControl above your table view and works as expected without having to use a UITableViewController :) 这会在表视图上方添加一个UIRefreshControl并按预期工作,而不必使用UITableViewController :)


    EDIT: This above still works but as a few have pointed out, there is a slight "stutter" when adding the UIRefreshControl in this manner. 编辑:上面这个仍然有效,但正如一些人所指出的那样,以这种方式添加UIRefreshControl时会有轻微的“口吃”。 A solution to that is to instantiate a UITableViewController, and then setting your UIRefreshControl and UITableView to that, ie: 解决方法是实例化UITableViewController,然后将UIRefreshControl和UITableView设置为,即:

    UITableViewController *tableViewController = [[UITableViewController alloc] init]; tableViewController.tableView = self.myTableView; self.refreshControl = [[UIRefreshControl alloc] init]; [self.refreshControl addTarget:self action:@selector(getConnections) forControlEvents:UIControlEventValueChanged]; tableViewController.refreshControl = self.refreshControl;

    #4楼

    What you would try is use container view inside ViewController you are using. 您将尝试使用您正在使用的ViewController中的容器视图。 you can define clean UITableViewController subclass with dedicated tableview and place that in the ViewController. 您可以使用专用的tableview定义干净的UITableViewController子类,并将其放在ViewController中。


    #5楼

    To eliminate the stutter that is caused by the accepted answer, you can assign your UITableView to a UITableViewController . 要消除由接受的答案引起的口吃,您可以将UITableView分配给UITableViewController 。

    _tableViewController = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain]; [self addChildViewController:_tableViewController]; _tableViewController.refreshControl = [UIRefreshControl new]; [_tableViewController.refreshControl addTarget:self action:@selector(loadStream) forControlEvents:UIControlEventValueChanged]; _theTableView = _tableViewController.tableView;

    EDIT: 编辑:

    A way to add a UIRefreshControl with no UITableViewController with no stutter and retain the nice animation after refreshing data on the tableview. 一种添加UIRefreshControl ,没有UITableViewController ,没有UIRefreshControl ,并在刷新tableview上的数据后保留好动画。

    UIRefreshControl *refreshControl = [UIRefreshControl new]; [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged]; [self.theTableView addSubview:refreshControl]; [self.theTableView sendSubviewToBack:refreshControl];

    Later when handling the refreshed data... 稍后处理刷新数据时......

    - (void)handleRefresh:(UIRefreshControl *)refreshControl { [self.theTableView reloadData]; [self.theTableView layoutIfNeeded]; [refreshControl endRefreshing]; }

    #6楼

    Try delaying the call to the refreshControl -endRefresh method by a fraction of a second after the tableView reloads its contents, either by using NSObject's -performSelector:withObject:afterDelay: or GCD's dispatch_after . 在tableView重新加载其内容之后, -performSelector:withObject:afterDelay:通过使用NSObject的-performSelector:withObject:afterDelay:或GCD的dispatch_after来延迟对refreshControl -endRefresh方法的调用,只需几分之一秒。

    I created a category on UIRefreshControl for this: 我为此在UIRefreshControl上创建了一个类别:

    @implementation UIRefreshControl (Delay) - (void)endRefreshingAfterDelay:(NSTimeInterval)delay { dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [self endRefreshing]; }); } @end

    I tested it and this also works on Collection Views. 我测试了它,这也适用于集合视图。 I've noticed that a delay as small as 0.01 seconds is enough: 我注意到延迟小到0.01秒就足够了:

    // My data refresh process here while the refresh control 'isRefreshing' [self.tableView reloadData]; [self.refreshControl endRefreshingAfterDelay:.01];
    Processed: 0.012, SQL: 9