LoginSignup
55
54

More than 5 years have passed since last update.

UITableViewやUICollectionViewでUIRefreshControlを使う

Posted at

iOS6 からメールアプリに実装されている「下に引っ張って更新」が
UIRefreshControl を使って簡単に実装できるので
UITableViewController や UICollectionViewController で使ってみる。

- (void)viewDidLoad
{
    // UIRefreshControl の初期化
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    self.refreshControl = refreshControl;
    [refreshControl addTarget:self action:@selector(refreshOccured:) forControlEvents:UIControlEventValueChanged];
    // tableViewの中身が空の場合でも UIRefreshControl を使えるようにする
    self.tableView.alwaysBounceVertical = YES;
    [super viewDidLoad];
}

- (void)refreshOccured:(id)sender
{
    [self xxx]; // 更新する処理を呼ぶ
    [self.refreshControl endRefreshing];
}

self.tableView.alwaysBounceVertical の設定を書かないと
tableView や collectionView の中身が
空の場合やセルの数が少ない場合に UIRefreshControl が機能しないので注意。

UITableView や UICollectionView をそのまま使っている場合は

[tableView addSubview:refreshControl];

のようにすれば実装できる。

55
54
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
55
54