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
.right
.bottom
思ったよりも違いが顕著で面白かったですが、普段使うのは無難な.fade
か、アニメーションのない.none
あたりが多そうです。
UITableView.RowAnimation - UITableView | Apple Developer Documentation