Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the List in SwiftUI reuse cells similar to UITableView?

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

like image 737
Let's_Create Avatar asked Jun 18 '19 18:06

Let's_Create


People also ask

How can we use a reusable cell in 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.

Can you reuse a view SwiftUI?

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.

How do I create a list in SwiftUI?

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!") }

How do I use Table View in 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.


1 Answers

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

like image 185
Mojtaba Hosseini Avatar answered Oct 17 '22 02:10

Mojtaba Hosseini