我有一个JSON API,我想显示PageTabView中的内容。它是成功的,但每次我滑动页面时,视图都会变得滞后,我在这里找到了解决方案https://stackoverflow.com/a/66591530/15132219.但现在的问题是,每次我刷页面时,页面点都不想改变。我已经尝试解决它几天了,但仍然找不到解决方案,这是我的代码(我无法显示API链接,因为它不可共享(
struct HomeBanner: View{
@State var banners: [BannerJSON] = []
private let timer = Timer.publish(every: 5, on: .main, in: .common).autoconnect()
@State var index = 0
var body: some View{
let bannerPic = "API_LINK"
GeometryReader{ geometry in
VStack(spacing: 2){
if banners.isEmpty{
Spacer()
ProgressView()
Spacer()
}else{
VStack{
ZStack{
TabView(selection: $index){
ForEach(banners, id: .id){banner in
VStack{
//MARK: Display Banner
NavigationLink(destination: DetailBanner(banner: banner
)){
WebImage(url: URL(string: bannerPic + banner.IMAGE))
.resizable()
.scaledToFit()
}
}
}
}.tabViewStyle(PageTabViewStyle())
.frame(width: geometry.size.width, height: geometry.size.height / 3.5)
}
}
}
}.onAppear{
getBannerData(url: "API_LINK"){
(banners) in
self.banners = banners
}
}
}
}
}
struct BannerJSON{
var RECORD_STATUS: String
var IMAGE: String
var URL_LINK: String
var TITLE: String
var DESCRIPTION: String
}
extension BannerJSON: Identifiable, Decodable{
var id: String {return TITLE}
}
struct SampleTabView: View {
@State var selectedIndex: Int = 0
@State var banners: [Int] = [0,1,2,3,4,5]
var body: some View {
TabView(selection: $selectedIndex){
ForEach(0..<banners.count, id: .self){ idx in
Text(banners[idx].description)
.tag(idx)//This line is crutial for selection to work put in on the VStack
//You can also make the tag the banner in your loop but your selection variable must be a Banner too.
}
}
}
}