API 尝试查看 TabView 时出错
Error about trying to view a TabView by API
我遇到了这个错误
Thread 1: Fatal error: Index out of range
我想通过TabView查看内容
次数来自API
的数据条数
他的图片背景显示在API
的数据数上
并让标签在这个数字上自动移动
此视图
import SwiftUI
struct Tap1Section1: View {
private let timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
@State private var currentIndex = 0
@State private var nummberIndex = 5
@StateObject var modelname = Api()
var body: some View {
VStack {
TabView(selection: $currentIndex) {
ForEach(modelname.models) { item in
Image("imgCollectionpdf")
.resizable()
.padding(.horizontal,5)
.overlay(
VStack {
Text(item.title)
.foregroundColor(.white)
.padding()
}
)
}
}.tabViewStyle(PageTabViewStyle())
.padding(.bottom,5)
.frame(width: .infinity, height: 140)
.onReceive(timer) { _ in
withAnimation {
currentIndex = currentIndex <
modelname.models.count ? currentIndex + 1 : 0
}
}
}
.onAppear {
modelname.getData()
}
}
}
从API获取数据,为真
你在这里找到了 modelname.models.count
尝试使用此示例代码在顶部显示您的 TabView
图片和号码。
根据您的目的调整代码。在获取数据时,您需要有一个视图。
使用 .tag
以便 TabView
知道您想看哪一个。
这只是一个示例,您应该 re-structure 您的代码,以显示
获取数据时的一些初始信息,例如。 ProgressView
,然后显示你的 TabView
.
struct Tap1Section1: View {
private let timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
@State private var currentIndex = -1 // <-- here for downloading
@StateObject var modelname = Api()
var body: some View {
VStack {
if currentIndex == -2 {
Text("no data from api") // <-- here for no data
} else if currentIndex == -1 {
ProgressView("downloading...") // <-- here downloading
} else {
TabView(selection: $currentIndex) {
ForEach(Array(modelname.models.enumerated()), id: \.offset) { offset, item in
Image("imgCollectionpdf")
.resizable()
.padding(.horizontal,5)
.overlay(Text(item.title).foregroundColor(.white))
.tag(offset) // <-- here
}
}.tabViewStyle(PageTabViewStyle())
.padding(.bottom,5)
.frame(maxWidth: .infinity, maxHeight: 140) // <-- here
}
}
.onReceive(timer) { _ in
withAnimation {
// -- here
currentIndex = currentIndex + 1 < modelname.models.count ? currentIndex + 1 : 0
if modelname.models.count == 0 { currentIndex = -2 }
}
}
.onAppear {
modelname.getData()
}
}
}
我遇到了这个错误
Thread 1: Fatal error: Index out of range
我想通过TabView查看内容
次数来自API
的数据条数他的图片背景显示在API
的数据数上并让标签在这个数字上自动移动
此视图
import SwiftUI
struct Tap1Section1: View {
private let timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
@State private var currentIndex = 0
@State private var nummberIndex = 5
@StateObject var modelname = Api()
var body: some View {
VStack {
TabView(selection: $currentIndex) {
ForEach(modelname.models) { item in
Image("imgCollectionpdf")
.resizable()
.padding(.horizontal,5)
.overlay(
VStack {
Text(item.title)
.foregroundColor(.white)
.padding()
}
)
}
}.tabViewStyle(PageTabViewStyle())
.padding(.bottom,5)
.frame(width: .infinity, height: 140)
.onReceive(timer) { _ in
withAnimation {
currentIndex = currentIndex <
modelname.models.count ? currentIndex + 1 : 0
}
}
}
.onAppear {
modelname.getData()
}
}
}
从API获取数据,为真
你在这里找到了 modelname.models.count
尝试使用此示例代码在顶部显示您的 TabView
图片和号码。
根据您的目的调整代码。在获取数据时,您需要有一个视图。
使用 .tag
以便 TabView
知道您想看哪一个。
这只是一个示例,您应该 re-structure 您的代码,以显示
获取数据时的一些初始信息,例如。 ProgressView
,然后显示你的 TabView
.
struct Tap1Section1: View {
private let timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
@State private var currentIndex = -1 // <-- here for downloading
@StateObject var modelname = Api()
var body: some View {
VStack {
if currentIndex == -2 {
Text("no data from api") // <-- here for no data
} else if currentIndex == -1 {
ProgressView("downloading...") // <-- here downloading
} else {
TabView(selection: $currentIndex) {
ForEach(Array(modelname.models.enumerated()), id: \.offset) { offset, item in
Image("imgCollectionpdf")
.resizable()
.padding(.horizontal,5)
.overlay(Text(item.title).foregroundColor(.white))
.tag(offset) // <-- here
}
}.tabViewStyle(PageTabViewStyle())
.padding(.bottom,5)
.frame(maxWidth: .infinity, maxHeight: 140) // <-- here
}
}
.onReceive(timer) { _ in
withAnimation {
// -- here
currentIndex = currentIndex + 1 < modelname.models.count ? currentIndex + 1 : 0
if modelname.models.count == 0 { currentIndex = -2 }
}
}
.onAppear {
modelname.getData()
}
}
}