简体   繁体   中英

In SwiftUI, How can I change the way List stack the row?

As default SwiftUI List stack the row by time, which results in the most recently created row is at the end of the list.

Instead I want most recently created row to be at upfront, the first row of the list.

Is there anyway to achieve this?

You can reverse the List so the most recent data is stored on top.

Here is an example with a button so you can add elements to the List and test it out.

struct ContentView: View {
    @State var dataStore = [0, 1, 2]

    var body: some View {
        VStack {
            List(dataStore.reversed(), id: \.self) { data in
                Text(String(data))
            }

            Button(action: {
                self.dataStore += [self.dataStore.count]
            }) {
                Text("Insert to list")
            }
        }.listStyle(GroupedListStyle())
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM