web-dev-qa-db-ja.com

UITableViewおよびUIRefreshControl

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];
}
14
gabac

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 へのクレジット。

32
Echelon

@Echelonの答えは的確ですが、ちょっとした提案が1つあります。後でアクセスできるように、更新コントロールを@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へのクラス全体のアクセスを提供するだけなので、endRefreshingを実行したり、UIRefreshControlのisRefreshingプロパティを確認したりできます。

10
self.name