简体   繁体   English

UITableView UIRefreshControl第一次不显示它的视图

[英]UITableView UIRefreshControl Does Not Show Its View The First Time

I have added the functionality of UIRefreshControl in my project that uses a UITableView. 我在使用UITableView的项目中添加了UIRefreshControl的功能。 The app works by fetching entries from a web service to a tableview. 该应用程序通过从Web服务获取条目到tableview来工作。 Below is the code i have used to add UIRefreshControl: 下面是我用来添加UIRefreshControl的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.tintColor = [UIColor grayColor];
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Updating New Entries"];
    [refreshControl addTarget:self action:@selector(pullToRefresh) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refreshControl;

    [self pullToRefresh];    
}

- (void) pullToRefresh
{
    counter = 1;
    [self fetchEntriesNew:counter]; // My code for updating table view

    [self performSelector:@selector(updateTable) withObject:nil afterDelay:2];
}

- (void)updateTable
{
    [self.tableView reloadData];
    [self.refreshControl endRefreshing];
}

Now if i pull to refresh, it refreshes by adding new entries if there are any and shows me the following view on top of the tableview: 现在,如果我进行刷新,则会通过添加新条目进行刷新(如果有),并在tableview上显示以下视图:

在此输入图像描述

Everything works great except when the app is launched or opened for the very first time, it does not show the view that i have showed in the above image, although it does refreshes the tableview. 一切都很好,除非第一次启动或打开应用程序,它没有显示我在上面的图像中显示的视图,虽然它确实刷新了tableview。 I want it to show the refresh control view every time it refreshes it. 我希望它每次刷新时都显示刷新控件视图。 Can anyone point out what i am doing wrong? 谁能指出我做错了什么? Thanks! 谢谢!

UPDATE: I have added [self refreshControl beginRefreshing] and the UIRefreshControl's spinner view is now showing but its above the first entry of the tableview. 更新:我添加了[self refreshControl beginRefreshing],UIRefreshControl的微调器视图现在显示,但它高于tableview的第一个条目。 Can anyone point out how to correct it? 任何人都可以指出如何纠正它?

This problem had really puzzled me for a while.I found that 4-inch iOS devices don't have this problem, but 3.5-inch devices do. 这个问题让我困惑了一段时间。我发现4英寸的iOS设备没有这个问题,但3.5英寸的设备却没有。

I tried to find out the differences between the first time that the refreshControl beginRefreshing itself and when I operated a pull gesture.It's the pull operation. 我试图找出第一次刷新控件beginRefreshing本身和我操作拉动手势之间的区别。这是拉动操作。

And I checked Apple's document on UIRefreshControl.It says The control does not initiate the refresh operation directly. Instead, it sends the UIControlEventValueChanged event when a refresh operation should occur. 我在UIRefreshControl上检查了Apple的文档。它说The control does not initiate the refresh operation directly. Instead, it sends the UIControlEventValueChanged event when a refresh operation should occur. The control does not initiate the refresh operation directly. Instead, it sends the UIControlEventValueChanged event when a refresh operation should occur.

So I thought maybe I could add something to simulate a pull gesture to trigger refreshControl's showing. 所以我想也许我可以添加一些东西来模拟一个拉动手势来触发refreshControl的显示。

[yourScrollView(or tableView) setContentOffset:CGPointMake(0.0f, -60.0f)
                                      animated:YES];
[yourRefreshControl beginRefreshing];

It works! 有用!

PS. PS。 UIRefreshControl works with UIScrollView, too. UIRefreshControl也适用于UIScrollView。 [yourScrollView addSubview:yourRefreshControl] just works. [yourScrollView addSubview:yourRefreshControl]正常工作。

我会将你的[self pullToRefresh]调用移动到viewWillAppear而不是viewDidLoad。

There are two things that can be done to add UIRefreshControl in your tableview neither of them is added in your code 在您的tableview中添加UIRefreshControl可以做两件事,它们都没有添加到您的代码中

1. [self setRefreshControl:tableRefreshControl];
2. [self.m_TableView addSubview:tableRefreshControl];

Either add 1 or 2 if your class is subclass of UIViewController 如果您的类是UIViewController的子类,则添加1或2

If your class is subclass of UITableViewController then try to replace 如果您的类是UITableViewController的子类,则尝试替换

self.refreshControl = refreshControl; with 2 line

before the code: 在代码之前:

[refreshControl beginRefresh] [refreshControl beginRefresh]

insert the code: 插入代码:

[refreshControl layoutIfNeeded] [refreshControl layoutIfNeeded]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM