LoginSignup
2
4

More than 5 years have passed since last update.

UITableViewのセルの再利用の正しいやり方

Posted at

正しいかもしれないですし、正しくないかもしれません。
わかる方教えてください

参考

https://developer.apple.com/reference/uikit/uitableview/1614878-dequeuereusablecell
https://developer.apple.com/reference/uikit/uitableview/1614891-dequeuereusablecell

本題

UITableView
    open func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.

    @available(iOS 6.0, *)
    open func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

 UITableVIewのdequeueReusableCellメソッドにindexPathを引数に持つものと持たないやつがいます。
動作は基本的に同じで、違うのは返り値がnilの可能性があるかないかです。(他にも違うところがあるかもしれませんが、調べた感じわかりませんでした。)

dequeueReusableCell(withIdentifier:for:)の方のメソッドを使えば毎回guardでnilチェックをしなくてもよくなりますね!

結論

dequeueReusableCell(withIdentifier:for:)を使いましょう!

2
4
3

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
2
4