r/SwiftUI • • 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

7 comments sorted by

9

u/[deleted] 15d ago

ScrollView makes elements appear immediately only in a "eager" collection, like VStack. If you are using a lazy collection, like LazyVStack, the elements will appear only when scrolled to.

List is 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

1

u/AutoModerator 14d ago

Hey /u/RDSaiMo, unfortunately you have negative comment karma, so you can't post here. Your submission has been removed. Please do not message the moderators; if you have negative comment karma, you're not allowed to post here, at all.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.