LoginSignup
8
8

More than 5 years have passed since last update.

【Swift4】TableViewの特定の行だけをリロードする、reloadRowsメソッドについて調べてみた

Last updated at Posted at 2019-02-02

reloadRowsメソッドの使い方

let indexPath = IndexPath(row: 1, section: 0)
tableView.reloadRows(at: [indexPath], with: .fade)

reloadRows(at:with:) - UITableView | Apple Developer Documentation

解説

第一引数 at: [indexPath]

リロードする行を指定します。
NSIndexPathオブジェクトの配列なので、下記のように複数行を指定することもできます。

let indexPath_1 = IndexPath(row: 1, section: 0)
let indexPath_2 = IndexPath(row: 2, section: 0)
tableView.reloadRows(at: [indexPath_1, indexPath_2], with: .fade)

NSIndexPath - Foundation | Apple Developer Documentation

第二引数 with: .fade

リロード時のアニメーションの仕方を指定します。
アニメーションにはいくつか種類があり、TableViewクラスの中で下記のように定義されています。

public enum RowAnimation : Int {

    case fade

    case right // slide in from right (or out to right)

    case left

    case top

    case bottom

    case none // available in iOS 3.0

    case middle // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy

    case automatic // available in iOS 5.0.  chooses an appropriate animation style for you
}

違いを見るために、reloadボタンを押すとTableViewの上5行がリロードされるサンプルを作ります。

.fade

よく見るリロードという感じ。
reload_fade.gif

.right

左から右へとリロードがかかります。
reload_right.gif

.bottom

上から下に向かってリロードがかかります。
reload_bottom.gif

思ったよりも違いが顕著で面白かったですが、普段使うのは無難な.fadeか、アニメーションのない.noneあたりが多そうです。

UITableView.RowAnimation - UITableView | Apple Developer Documentation

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