5

我在使用 UITableView 的项目中添加了 UIRefreshControl 的功能。该应用程序通过将条目从 Web 服务获取到 tableview 来工作。下面是我用来添加 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];
}

现在,如果我拉刷新,它会通过添加新条目(如果有)来刷新,并在 tableview 顶部显示以下视图:

在此处输入图像描述

一切都很好,除了第一次启动或打开应用程序时,它没有显示我在上图中显示的视图,尽管它确实刷新了 tableview。我希望它每次刷新时都显示刷新控件视图。谁能指出我做错了什么?谢谢!

更新:我添加了 [self refreshControl beginRefreshing] 并且 UIRefreshControl 的微调器视图现在显示,但它在 tableview 的第一个条目之上。谁能指出如何纠正它?

4

4 回答 4

12

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.

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.

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.

So I thought maybe I could add something to simulate a pull gesture to trigger refreshControl's showing.

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

It works!

PS. UIRefreshControl works with UIScrollView, too. [yourScrollView addSubview:yourRefreshControl] just works.

于 2013-10-19T04:38:51.837 回答
1

我会将您的 [self pullToRefresh] 调用移至 viewWillAppear 而不是 viewDidLoad。

于 2013-06-19T13:55:17.530 回答
1

有两件事可以在你的 tableview 中添加 UIRefreshControl,它们都没有添加到你的代码中

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

如果您的类是 UIViewController 的子类,则添加 1 或 2

如果您的类是 UITableViewController 的子类,请尝试替换

self.refreshControl = refreshControl; with 2 line
于 2013-06-19T14:29:23.950 回答
0

代码前:</p>

[刷新控制开始刷新]

插入代码:</p>

[refreshControl layoutIfNeeded]

于 2017-02-03T12:55:59.797 回答