8

According to the official documentation, there are two ways to get a reusable cell from a queue of a tableView. One is dequeueReusableCell(withIdentifier:for:) and another is dequeueReusableCell(withIdentifier:). Assuming from the explanation of the document, I think the former is the method that returns the reusable cell and adds it to tableView. On the other hand, the latter is the method that just returns the reusable cell. Is this right?

If it is right, I have another question.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell = self.tableView.dequeueReusableCell(with: SomeTableViewCell.self, for: indexPath)
    let anotherCell: UITableViewCell = self.tableView.dequeueReusableCell(with: AnotherTableViewCell.self)
    return anotherCell
}

At first line, we can get the reusable cell and add the cell to the tableView. And at the second one, we just obtain another reusable cell. Finally, the method returns the cell obtained from the second line. The returned cell is different with the cell being having added to the tableView already. In this case, the cell added to tableView at first line is just replaced by the returned cell at the final line? Thanks.

Harshal Valanda
  • 5,331
  • 26
  • 63
Kazuya Tomita
  • 667
  • 14
  • 34
  • 4
    Possible duplicate of [When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier : forIndexPath](https://stackoverflow.com/questions/25826383/when-to-use-dequeuereusablecellwithidentifier-vs-dequeuereusablecellwithidentifi) – Saurabh Bhatia May 27 '17 at 06:53
  • You can take a reference from this answer - https://stackoverflow.com/a/25835438/5237727 – Saurabh Bhatia May 27 '17 at 07:02
  • Have you seen the explanation of the documentation? The answer you showed can be the answer. However, it doesn't cover this question. I focus the point on the explanation that one just returns cell and another returns the cell and adds it to table view. And I want to ask whether my interpretation is right or not. – Kazuya Tomita May 27 '17 at 07:08

4 Answers4

14

Method dequeueReusableCell(withIdentifier:) is older than dequeueReusableCell(withIdentifier:for:), and the main difference between them is that first will return nil, if cell don't registered, the second will throw an exception, and an app will crash. IndexPath requires for height calculation(if defined tableView:heightForRowAtIndexPath)

Stan H
  • 206
  • 2
  • 6
  • 1
    Thanks, the document says "Returns a reusable table-view cell object for the specified reuse identifier and adds it to the table" as the explanation of the `dequeueReusableCell(withIdentifier:for:)`. What do you think about it? – Kazuya Tomita May 27 '17 at 07:11
  • 2
    it's means if this type cell is not created, or table view do not have enough instances of this cell, it will create it, otherwise it will return already created instance for reuse. – Stan H May 27 '17 at 07:16
  • 1
    I can understand gradually, but what is exactly "the table" in "Returns a reusable table-view cell object for the specified reuse identifier and adds it to the table" ? Isn't it the table view? – Kazuya Tomita May 27 '17 at 07:25
  • 1
    this cell will store in table view(cell will be created if required), but it's not going to display on screen only if you say this to do, by returning this cell in your `tableView(_:cellForRowAt:)` – Stan H May 27 '17 at 07:32
  • Thanks. Very understandable. – Kazuya Tomita May 27 '17 at 13:04
  • You are welcome. Also please up vote if my answer helped you. – Stan H May 27 '17 at 13:08
0

After having a look at the official documentation

 I realised that your interpretation is somewhat correct.

Could you please have a look at this answer, as this might bring up more clarity.


Saurabh Bhatia
  • 1,875
  • 1
  • 20
  • 29
  • Could you see https://developer.apple.com/reference/uikit/uitableview/1614878-dequeuereusablecell? The explanation seems to contradict with your answer because you say the obtained cell is never added to the table view and the document says returns the cell and adds it to the table view. – Kazuya Tomita May 27 '17 at 07:20
0

dequeueReusableCell(withIdentifier:) AND dequeueReusableCell(withIdentifier:for:)

Both returning the cell, but older method return nil while latest method crashes the app.

older may support upto iOS 5 while newer method support iOS 6 and Above

Aashish
  • 161
  • 13
0

If you will use below method you always get the initialized instance and it will always be the right size for that indexpath, so you will be able to do layout inside your contentview knowing that the size is correct. As this will set the cell size before returning it that's why we need to add indexPath.

let cell: UITableViewCell = self.tableView.dequeueReusableCell(with:
  "Cell", for: indexPath)

In below case you have to check if the cell is nil, configure it yourself.

    var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "Cell")

    if (cell == nil) {

      cell = UITableViewCell(style:UITableViewCellStyle.subtitle, reuseIdentifier:"Cell")
   }

Note: In newer version i.e. self.tableView.dequeueReusableCell(with: "Cell", for: indexPath) app crashes if you didn't register a class/nib for the identifier. In older version i.e. tableView.dequeueReusableCell(withIdentifier: "Cell") version returns nil in that case. Moreover if you are using storyboard you don't need to worry about registering the cell.

dequeueReusableCellWithIdentifier:forIndexPath: will always return a cell. On the other hand dequeueReusableCellWithIdentifier: will return nil if no reusable cell that's why nil check is required.

Maddy
  • 1,660
  • 11
  • 24