19

I am working on creating a dynamic list using SwiftUI. Does the SwiftUI List container reuse the cells similar to UITableView?

Gene Z. Ragan
  • 2,643
  • 2
  • 31
  • 41
Let's_Create
  • 2,963
  • 3
  • 14
  • 33
  • 1
    Why do you ask? I think `SwiftUI` promises to be performant; whether it reuses views in this specific way is an implementation detail. – beyowulf Jun 18 '19 at 19:06
  • 3
    Check the memory graph - it does – Matteo Pacini Jun 18 '19 at 19:23
  • 1
    Sorry to chime in, but I just linked to this question because it seems someone else *may* be wondering something close. I was focused on "dynamic" - State (SO interpreted the "at sign" as a user!), array updates, etc. - and it actually may be the question is more concerned with "reusable". I say (at least for now) leave it up. @Matteo_Pacini has been giving quite a few **great** answers about SwiftUI and Combine.. Maybe he could give another one? I'd surely upvote it. –  Jun 18 '19 at 20:38
  • 1
    Check these two answers for more information around this topic: https://stackoverflow.com/a/56652113/5623035 - https://stackoverflow.com/questions/56654877 – Mojtaba Hosseini Jun 18 '19 at 21:34

1 Answers1

31

Yes, List is reusing its ListCoreCellHosts exactly like the way UITableView reuses its UITableViewCells.

Reference:

Investigating memory usage with Xcode shows that, when the number of the items is more than List could present at once, it just shows as much as it can and reuses them when they become occluded from the top or bottom of the list.

enter image description here

By tracing a single cell memory address, you can see it is reused over and over. Another exciting tidbit is that ListCoreCellHost uses a hosting view that may refer to UIKit internally. (Not known well because it lacks documentation)

enter image description here

Velociround
  • 611
  • 7
  • 18
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • By checking the addresses like you suggest, I find that the hosting views and subviews are not reused, only the cell itself. – rens Nov 30 '22 at 17:29