From the course: Complete Guide to SwiftUI
Populating collections with List
From the course: Complete Guide to SwiftUI
Populating collections with List
- [Instructor] We've used ForEach and ScrollView for our list so far. However, there's a more robust way of handling data collections, and that is a List. To start using Lists, our code needs only one change. I need to replace ScrollView with Lists. So let's go over to MenuView here and try that out. So we go to MenuView over here, and here's the ScrollView. I can make this really easy into a List by just doing this. And then click out here, and you can see we got a list. And it looks a little different. It's got little dividers in here, and it still scrolls the same way though. It's got a couple other interesting features which we haven't played with yet. But List is a lot more powerful than ForEach. You may use them together, like I did here, where you'll have a List and then a ForEach. Or you may use List by itself as a replacement for ForEach. And we're going to do both of those in this case. One of the things that a List can do that a ForEach can't is you can break your menu into titled sections. And there's several versions of sections, but in all the versions, you iterate over the sections, and then iterate within the section rows. Some datasets will have arrays within the struct, and then you use one set of logic to go with those. The other approach is one I'm going to be taking is iterate over enums that adopt the CaseIterable protocol. So if you go take a look over here at MenuItem, you'll see that my enums here, which we're going to be using the MenuCategory one, has this CaseIterable. And if you have this in place, it means you can use a ForEach or a List to list all these out with a special property. So let's go back into our code again, and I'm going to go try that now. So List, like I said, works like ForEach. So I can actually do it up into here. So what I'm going to use here is that MenuCategory. So I can go List, and there's MenuCategory. And I can use allCases, as long as I've used that CaseIterable protocol. And then over here, I can put in category in. And it's still going to give you some errors because all cases, I'm going to need that id equals self thing again. So it's not identifiable. And then once we have that, it's now going to use the ForEach to iterate through these, which is still fine. But what I'm going to do is I'm going to put a section around the ForEach. So let's do this. I'm going to do a Command-click. And we'll do an Embed. And the container we're going to use is Section. And Section used this way takes closures returning a view after the content of this section as a header, a footer, or both. Now, I'm going to use a header here. So let's go to the List one for this one. Right here is the parentheses we need. And I'm going to put in header. I'm going to make that Text. And that'll be a category coming from the List. And these are enums, but I made them strings. So the raw value is a string. So I can just do rawValue here. And you could see on the top here, we've got Huli Pizza Original. So if you scroll enough, you'll start seeing the other topics. And we get the Mainland Specialty Pizzas and so on. Now, the problem here is, of course, it's showing all the pizzas. I don't want to show all the pizzas. I want to just show some of the pizzas, the ones that are for that category. So I'm going to go into the ForEach here, and I'm going to make a change is to use a filter to find only things within that category. And so we'll do $0 category == category. So that filters for only that category. And you can see on the side here now that we have the Italian Specialty Pizzas, the Mainland Pizzas, and the Huli Pizza Originals all in the right places. Okay, so we've got it working. We've now broken 'em down to sections. And we could see it over here in ContentView. We get the whole effect. And now we have the whole effect here where we can see all of that in place. Now, Lists have a lot more features you can use, and we'll look at a little more of them as we get along in the course.
Contents
-
-
-
-
-
-
(Locked)
MVC vs. MVVM4m 19s
-
(Locked)
State variables and buttons3m 57s
-
(Locked)
Challenge: Add a button45s
-
(Locked)
Solution: Add a button3m 50s
-
(Locked)
Use Binding variables10m 10s
-
(Locked)
The Identifiable protocol5m 13s
-
(Locked)
Add a model to root views6m 47s
-
Populating collections with List4m 45s
-
(Locked)
Selection and binding variables6m 29s
-
(Locked)
-
-
-
-
-
-
-
-
-
-
-
-