Pulltorefresh 只工作一次而不是多次

Pulltorefresh works only once not multiple times

我有一个带有原型单元格的表格视图。

我的 viewDidLoad 方法中有以下代码,

_refreshControl = [[UIRefreshControl alloc]init];

[_refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = _tblView;
tableViewController.refreshControl = _refreshControl;

以下是我的 refreshData 方法....

 -(void)refreshData
{
    [_request getApproveStatutoryMapping];
    UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = _tblView;
[_tblView reloadData];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM d, h:mm a"];
NSString *title = [NSString stringWithFormat:@"Last update: %@", [formatter stringFromDate:[NSDate date]]];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
                                                            forKey:NSForegroundColorAttributeName];
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary];
tableViewController.refreshControl.attributedTitle = attributedTitle;

[tableViewController.refreshControl endRefreshing];

}

问题在于

  1. attributedTitle 不可见
  2. 拉动刷新只起作用一次,如果我再次拉动它,它就不起作用。为什么会这样?

您将 UITableViewController *tableViewController 定义为 UIViewController

的 属性

在方法refreshData中删除行

UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = _tblView;

方法 endRefreshing 必须在您刷新数据后调用,而不是在您开始刷新数据时调用,否则您会看到 pre-loader 不到一秒就没有等待更新

这里

NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
                                                            forKey:NSForegroundColorAttributeName];

您将刷新标题的颜色定义为白色,如果背景是白色,您将永远看不到标题

我遇到了同样的问题,下拉刷新只工作了一次。经过大量的努力,我得到了一个解决方案,即在将数据加载到表视图之前,我们需要停止拉动以刷新。

refreshControl.endRefreshing()

希望对您有所帮助。