I am working on creating a dynamic list using SwiftUI. Does the SwiftUI List
container reuse the cells similar to UITableView
?
For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.
SwiftUI makes it easy to refactor and reuse code in many features. If both views only used List to display gems, with no other view inside, it would be straightforward to extract the code into another view and use it in both features.
Probably the simplest way to build a list is to create a new SwiftUI view and wrap the Hello World text in a List: struct StaticListView: View { var body: some View { List { Text("Hello, world!") } } } To add more items to the list, we can just add another line: List { Text("Hello, world!") Text("Hello, SwiftUI!") }
Open Xcode and select Create a new Xcode project. And then fill in the details for the new project, let's name the app “RestaurantMenu” and make sure you've selected SwiftUI for the User Interface. And click Next. And then choose your preferred directory and click on Create to create your new Project.
Yes, List
is reusing its ListCoreCellHost
s exactly like the way UITableView
reuses its UITableViewCell
s.
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
.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With