r/SwiftUI • u/beatpaq • 2d ago
The difference between List and ScrollView when it comes to loading elements?
It seems like ScrollView makes all of its child elements immediately visible while latter elements of list won't trigger .onAppear. Is there a way to make ScrollView behave like a List?
6
Upvotes
10
u/Extra-Ad5735 2d ago
ScrollView makes elements appear immediately only in a "eager" collection, like
VStack. If you are using a lazy collection, likeLazyVStack, the elements will appear only when scrolled to.Listis basically a table: when you have elements of the same size, which potentially can be editable in place. ScrollView only scrolls whatever container lives inside of it, not giving much regard to what it consists of.5
u/brkaydev 2d ago
Put a LazyVStack inside the ScrollView instead of a VStack. A plain VStack builds every child up front, which is why all of them fire their appear handler right away. LazyVStack only creates rows as they come near the screen, so it behaves like List there, and you can trigger the next page load when the last row appears.
2
u/cuiyingjie 1d ago
One important caveat on top of using `LazyVStack`: it loads rows lazily like `List`, **but it doesn't recycle cells the way `List` (`UICollectionView`) does**. If your feed grows to hundreds of rows (especially with images), memory keeps climbing in a `ScrollView + LazyVStack` unless you window/cap what's in memory, whereas `List` reuses cells automatically.
Also, if you're relying on `.onAppear` for pagination or read-tracking, keep in mind `LazyVStack` can fire `.onAppear` slightly early and repeatedly as you scroll back and forth. On iOS 18+, `.onScrollVisibilityChange` is much cleaner when you care about what's actually visible on screen (and `.task` if you want async loads to auto-cancel when a row scrolls away).
0
u/[deleted] 2d ago edited 2d ago
[removed] — view removed comment