Create a Grid View that allows items to be processed lazily before being displayed. This pod does not use LazyVGrid
or LazyHGrid
.
SwiftUILazyGridView is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SwiftUILazyGridView'
✅ Data source is maintained by the Grid View's ViewModel
, so yo don't have to fiddle with SwiftUI States
✅ Compatible with different orientations
✅ Variables column and item spacing
✅ Supports all iOS versions that are compatible with SwiftUI
⏳ Lazy loading of items (only processing is supported) like SwiftUI's LazyVGrid
⏳ Fixed item size with variable columns
struct ContentView: View{
var viewModel = LazyGridViewModel<Int, String>(UIScreen.main.bounds.width - 10.0, spacing: 0.0)
init(){
setupData()
}
private func setupData(){
for i in 0..<100{
viewModel.addItem(i)
}
}
var body: some View{
LazyGridView<Int, String>(viewModel) { (input, callback) in
// Processing closure
let processedString = String(format: "Number %d", input)
callback(processedString)
} _: { (processed) -> AnyView in
// View Builder closure
return AnyView(
Text(processed)
)
} _: { (clickedItem) in
guard let index = viewModel.getAllItems().firstIndex (where: { $0?.id == clickedItem?.id }) else { return }
print("You clicked the item at index, \(index)")
self.addRandomItem()
}
}
}
struct CustomObject{
var id: Int
var name: String
}
struct ContentView: View{
var viewModel = LazyGridViewModel<CustomObject, String>(UIScreen.main.bounds.width - 10.0, spacing: 0.0)
init(){
setupData()
}
private func setupData(){
for i in 0..<100{
let c = CustomObject(id: i, name: "Random Name")
viewModel.addItem(c)
}
}
var body: some View{
LazyGridView<Int, String>(viewModel) { (input, callback) in
// Processing closure
DispatchQueue.global().async {
// Simulate long running task
let randomDelay = arc4random_uniform(10000000)
usleep(randomDelay)
// Processing complete!
callback(input.name)
}
} _: { (processed) -> AnyView in
// View Builder closure
return AnyView(
Text(processed)
)
} _: { (clickedItem) in
guard let index = viewModel.getAllItems().firstIndex (where: { $0?.id == clickedItem?.id }) else { return }
print("You clicked the item at index, \(index)")
self.addRandomItem()
}
}
}
- iOS 13.0+
- Xcode 11
SwiftUILazyGridView is available under the MIT license. See the LICENSE file for more info.