14

我正在尝试将 UIRefreshControl 移动到我的 headerView 之上,或者至少让它与 contentInset 一起使用。有谁知道如何使用它?

在 TableView 中滚动时,我使用 headerView 来获得漂亮的背景。我想要一个可滚动的背景。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Set up the edit and add buttons.

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundColor = [UIColor clearColor];

[self setWantsFullScreenLayout:YES];

self.tableView.contentInset = UIEdgeInsetsMake(-420, 0, -420, 0);

UIImageView *top = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top.jpg"]];
self.tableView.tableHeaderView = top;

UIImageView *bottom = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottom.jpg"]];
self.tableView.tableFooterView = bottom;

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"settingsIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(showSettings)];
self.navigationItem.leftBarButtonItem = leftButton;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addList)];
self.navigationItem.rightBarButtonItem = addButton;

//Refresh Controls
self.refreshControl = [[UIRefreshControl alloc] init];

[self.refreshControl addTarget:self action:@selector(refreshInvoked:forState:) forControlEvents:UIControlEventValueChanged];
}
4

2 回答 2

32

我不太确定您对 contentInset 的意图是什么,但就将 UIRefreshControl 添加到 UITableView 而言,可以做到。UIRefreshControl 实际上是与 UITableViewController 一起使用的,但如果你只是将它作为子视图添加到 UITableView 中,它就会神奇地工作。请注意,这是未记录的行为,可能在另一个 iOS 版本中不受支持,但它是合法的,因为它不使用私有 API。

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

- (void)handleRefresh:(id)sender
{
    // do your refresh here...
}

感谢@Keller注意到这一点

于 2012-10-16T15:56:20.997 回答
10

@Echelon's answer is spot on, but I have one minor suggestion. Add the refresh control as a @property so you can access it later.

in YourViewController.h

@property (nonatomic, strong) UIRefreshControl *refreshControl;

in YourViewController.m

-(void) viewDidLoad {
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:self.refreshControl]; //assumes tableView is @property
}

And the big reason to do it this way...

-(void)refresh {
    [self doSomeTask]; //calls [taskDone] when finished
}

-(void)taskDone {
    [self.refreshControl endRefreshing];
}

Just gives you class-wide access to the UIRefreshControl so you can endRefreshing or check the isRefreshing property of UIRefreshControl.

于 2014-01-29T21:16:22.870 回答