mirror of
https://github.com/hanzoskill/pagerkit.git
synced 2026-10-30 21:05:53 +00:00
2.3 KiB
2.3 KiB
ForEach in PagerKit
ForEach in PagerKit is a component for iterating over a collection to generate PKPage views dynamically.
Usage
Use ForEach inside a PKPagesView to create pages from a data source. It works similarly to SwiftUI's ForEach.
With Identifiable Data
If your data model conforms to Identifiable, you can use the simpler initializer.
import SwiftUI
import PagerKit
struct Item: Identifiable {
let id = UUID()
let title: String
}
struct ContentView: View {
let items = [Item(title: "Home"), Item(title: "Profile"), Item(title: "Settings")]
var body: some View {
PKPagesView {
ForEach(items) { item in
PKPage {
Text(item.title)
}
}
}
}
}
With a Custom ID Key Path
If your data is not Identifiable, you can provide a key path to a unique Hashable property.
import SwiftUI
import PagerKit
struct Item {
let name: String
let uniqueId: Int
}
struct ContentView: View {
let items = [Item(name: "Home", uniqueId: 1), Item(name: "Profile", uniqueId: 2), Item(name: "Settings", uniqueId: 3)]
var body: some View {
PKPagesView {
ForEach(items, id: \.uniqueId) { item in
PKPage {
Text(item.name)
}
}
}
}
}
Initializers
init(_:content:): For collections ofIdentifiableelements.init(_:id:content:): For collections where you specify a custom identifier key path.
Key Concepts
- Data-driven pages:
ForEachallows you to create a pager whose content is driven by your app's data. - Dynamic content: The pager will update automatically if the data collection changes.
- Result Builder Integration:
ForEachworks seamlessly within thePKPageBuilderresult builder used byPKPagesView.
Best Practices
- Prefer
Identifiable: Whenever possible, make your data models conform toIdentifiablefor cleaner code. - Use a stable and unique ID: The identifier you provide should be unique for each element and stable across data updates to avoid unexpected UI behavior.
- Combine with static pages: You can mix
ForEachwith staticPKPageviews inside the samePKPagesView.