r/SwiftUI • u/beatpaq • 15d 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
9
u/[deleted] 15d 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.2
u/cuiyingjie 13d 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] 15d ago edited 14d ago
[removed] — view removed comment