UITableView和UIRefreshControl

14

我正尝试将UIRefreshControl移动到HeaderView的顶部,或者至少使其能够与contentInset一起使用。有人知道如何使用它吗?

我使用了一个headerView,在TableView中滚动时可以拥有漂亮的背景。我希望拥有可滚动的背景。

- (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];
}
2个回答

32

我不太确定您使用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...
}

鸣谢@Keller发现了这个问题


3
在普通的UIViewController中,您没有self.refreshControl,所以您完全是错误的。 - Godfather
1
如果您的TableView有一个标题视图(至少在iOS7上),则此方法存在一些问题。当您在刷新时向下滚动时,表格部分会全部错位,偏移了标题视图的高度。 - AlBeebe

10

@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属性。

1
@property (nonatomic,strong) UIRefreshView *refreshControl; 这里您可能是想用 UIRefreshControl 吧?对吗? - Emil Ahlbäck
@self.name 完成后为什么要调用 taskDone?这让我很惊讶。实际上,它应该执行 taskDone 然后再返回来刷新,对吗?感谢你的回答,点个赞。 - Ron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接