Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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

参考

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:)を使いましょう!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした