Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit element in ForEach in Swift Ui

I can't limit my forEach for only 3 items inside my list.

I tried to group them or to just limit the list with only 3 items but I don't have the result I want. I have all entries of Firestore document.

import SwiftUI
import Firebase
import Ballcap

struct MainPage : View {

    @ObjectBinding var dataSource: ItemDatabase = ItemDatabase()

    var item: Item

    var body: some View {
        NavigationView {
            List {
                ForEach(self.dataSource.items.identified(by: \.id)) { item in
                                    ShortArticleItem(item: item.data!)
                                        .padding()
                                }
                VStack(alignment: .leading) {
                            Text(self.item.categorie ?? "")
                                .font(.largeTitle)
                                .fontWeight(.black)
                                .multilineTextAlignment(.leading)
                                .foregroundColor(.black)

                            ForEach( self.dataSource.items.identified(by: \.id)) { item in
                                            MediumArticleItem(item: item.data!)
                    }
                }
            }
            .navigationBarTitle(Text("The News Place"))
        }
    }

I would like to have just the 3 last elements in my ForEach

like image 370
Mathieu Cloart Avatar asked Jan 26 '23 00:01

Mathieu Cloart


1 Answers

If you are using Array you can always use the array function prefix(number) to determine how many items you want to render


ForEach(arrayItem.prefix(5), id: \.id){ item in 
  //DO Something Here
}
like image 122
JPilson Avatar answered Feb 04 '23 03:02

JPilson