9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Swift】UIRefreshControlを使ってTableViewを下に引っ張ったときに読み込まれるやつを作りたい

Posted at

TableViewを下に引っ張ったときに読み込まれるやつ

twitterとかにあるやつのこと!
tableviewを下に引っ張るとクルクルが出てきて再度読み込まれるっていうアレです
IMG_1544F97FE10F-1.jpeg

qiitaとかネットでいろいろ調べたんだけど、それだとちゃんと動かなかったのでここに書いときます
この記事ではTableViewで使う時のことを書きますが、iOS10からスクロールするViewならなんでもつけることができるようになったみたいです!

TableViewにUIRefreshControlを追加して、AddTargetする

tableView.refreshControl = UIRefreshControl()
tableView.refreshControl?.addTarget(self, action: #selector(refresh), for: .valueChanged)

これでもうTableViewにくるくるが追加されました

くるくるした時の処理を書く

@objc func refresh() {
    /*
        ここに処理を書く
    */
    tableView.refreshControl?.endRefreshing()    //これを呼び出すとくるくるが止まる
}

refreshControl.endRefreshing()を呼んであげないと、永遠にくるくるし続けるので注意!
呼び出すタイミングは処理が終わったタイミングがいいですね!

DispatchQueue

とか

Timer.scheduledTimer

とかを使ってあげるのがいいと思う

よきくるくるライフを!

結構簡単に実装できて嬉しいですね!

環境

Xcode 11.5

参考

9
7
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
9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?