UITableView和UIRefreshControl

UITableView和UIRefreshControl,uitableview,uirefreshcontrol,Uitableview,Uirefreshcontrol,我正在尝试将UIRefreshView控件移动到headerView的顶部,或者至少让它与contentInset一起工作。有人知道怎么用吗 在TableView中滚动时,我使用了headerView以获得良好的背景。我想要一个可滚动的背景 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // Set up the

我正在尝试将UIRefreshView控件移动到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];
}

我不确定您对contentInset的意图是什么,但就向UITableView添加UIRefreshControl而言,这是可以做到的。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...
}

谢天谢地。

@Echelon的答案很准确,但我有一个小建议。将刷新控件添加为@property,以便以后可以访问它

在YourViewController.h中

@property (nonatomic, strong) UIRefreshControl *refreshControl;
在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
}
这样做的主要原因是

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

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

只允许您在类范围内访问UIRefreshControl,以便您可以结束刷新或检查UIRefreshControl的isRefreshing属性。

在普通UIViewController中,您没有self.refreshControl,因此您完全错了Lensovett如果您的tableview具有标题视图(至少在iOS7上),则此方法会出现一些问题。当您在刷新时向下滚动时,表格部分会被标题视图的高度所偏移,完全错位。
@property(nonatomic,strong)UIRefreshView*refreshControl
您可能是指这里的
UIRefreshControl
。对吗?@self.name为什么在完成后调用
taskDone
?我很惊讶。事实上,它应该执行
taskDone
并再次刷新,对吗+1.谢谢你的回答。