LoginSignup
18
13

More than 5 years have passed since last update.

【Swift4】UITableViewで一番下までスクロールしたことを検知し、イベント発火させる手法

Last updated at Posted at 2018-07-17

日本語でぐぐったらこれじゃないって検索結果がでたので。
英語でぐぐったらいい感じの記事があった。
https://stackoverflow.com/questions/46208539/how-to-determine-if-the-last-cell-of-a-uitableview-is-visible-on-the-screen?noredirect=1&lq=1

下記の通り

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        guard let lastCell = tableView.cellForRow(at: IndexPath(row: tableView.numberOfRows(inSection: 0)-1, section: 0)) else {
            return
        }
        // ここでリフレッシュのメソッドを呼ぶ
    }

リフレッシュメソッドを呼ぶタイミングが一番下についてからだとちょっと遅く感じるかもしれない
このようにすると、付く前に呼ばれる
また、lastCellが不要な場合はそもそも宣言しなくてよい

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // 下から5件くらいになったらリフレッシュ
        guard tableView.cellForRow(at: IndexPath(row: tableView.numberOfRows(inSection: 0)-5, section: 0)) != nil else {
            return
        }
        // ここでリフレッシュのメソッドを呼ぶ
    }
18
13
1

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
18
13