Implement pull-to-refresh and load-more with RefreshHeader and RefreshFooter
masterRefresh provides SwiftUI-native components for handling data refreshing and pagination within a ScrollView.
Key Components:
RefreshHeader: Handles drop-down pull-to-refresh logic. It requires a binding to a refreshing state (refreshing:), an asynchronous or synchronousactionto perform the reload, and a view builder to customize the refresh indicator UI.RefreshFooter: Handles scroll-up to load more logic. It requires a binding to a refreshing state (refreshing:), anactionto load more data, and a view builder for the footer UI..noMore(Bool): A modifier forRefreshFooterto indicate when no further data is available..preload(offset: Int): A modifier forRefreshFooterto trigger the load-more action before the user reaches the absolute bottom, based on a pixel offset..enableRefresh(): A modifier applied to theScrollViewto enable the refresh capabilities.
ScrollView {
RefreshHeader(refreshing: $headerRefreshing, action: reload) {
if self.headerRefreshing {
Text("refreshing...")
} else {
Text("Pull to refresh")
}
}
ForEach(items) { item in
YourCell(item: item)
}
RefreshFooter(refreshing: $footerRefreshing, action: loadMore) {
if self.noMore {
Text("No more data !")
} else {
Text("refreshing...")
}
}
.noMore(noMore)
.preload(offset: 50)
}
.enableRefresh()