admin管理员组

文章数量:1293702

I am developing a SwiftUI app that displays a list of images using List and AsyncImage. However, I noticed that List keeps the cells in memory even when they scroll off-screen. I expected it to remove off-screen cells and reload them when they come back into view, but instead, it seems to cache them.

I did not set up URLCache, so the images should be loaded every time they appear on screen.

Here is my code:

NavigationStack {
  List {
    ForEach(viewModel.images) { image in
      AsyncImage(url: URL(string: data.imageURL)) { phase in
        let imageSize: CGFloat = 100
                  
        if let image = phase.image {
          image.resizable()
            .scaledToFill()
            .frame(width: imageSize, height: imageSize)
            .clipped()          
        } else if phase.error != nil {               
          Text(phase.error?.localizedDescription ?? "unexpected error")
            .foregroundColor(Color.pink)
            .frame(width: imageSize, height: imageSize)
        } else {
          ProgressView()
            .frame(width: imageSize, height: imageSize)
        }      
      }
   }
}

本文标签: swiftSwiftUI List Doesn39t Remove OffScreen Cells When ScrollingStack Overflow