UIRefreshControlの使い方

引っ張って更新を簡単に実装するためのコントロール

@property (retain, nonatomic) UIRefreshControl *refreshControl;
@synthesize refreshControl = _refreshControl;

しておき,viewDidLoadとかで

_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
[self.collectionView addSubview:_refreshControl];

する.

使い方をまとめると
1. 初期化
2. addTerget action forControlEventsでターゲットアクションを追加できる.複数のイベントを追加可能.
3. addSubViewでベースとなるView(TableViewやCollectionView)に追加する.Viewのサブクラスのみ,勘違いしやすそうだけどViewControllerのサブクラスではない.

actionはメソッド@selectorで追加.ここで追加したメソッドが更新処理の本体となる.

UIControlEventValueChangedは引っ張って離したときのイベント.

UITableViewやUICollectionViewはreloadDataメッセージを送信するとデータを読み込み直すので大抵の場合これを使う.

_refreshControlにendRefreshメッセージを送信すると終了する.