我有这个问题,我在循环一些数据,然后在子视图中我从 API 加载一些额外的数据。根据从该 API 返回的数据,我希望逻辑检查是否显示它。
我尝试了不同的解决方案,但似乎无法解决问题。
任何帮助都会很棒。
struct WatchlistGridView: View {
@Environment(\.horizontalSizeClass) var horizontalSizeClass
@ObservedObject var watchlistVM = WatchlistViewModel()
@Binding var watchlistData: [WatchlistItemViewModel]
var width: CGFloat = 69 * 2
var height: CGFloat = 103 * 2
var cornerRadius: CGFloat = 8
@Binding var showToast: Bool
@Binding var toastTitle: String?
@Binding var toastSubTitle: String?
@Binding var toastImage: String?
@Binding var toastImageColor: Color?
var body: some View {
LazyVGrid(
columns: [GridItem(.adaptive(minimum: width), spacing: 0)], spacing: 16){
ForEach(self.watchlistData, id:\.self) { item in
SubGridView(item: item)
}
}
.padding(.leading)
.padding(.trailing)
}
}
struct SubGridView: View {
@ObservedObject var movieLogicState = MovieDetailLogic()
@State var isLoading: Bool = false
@State var tmdbItem: TMDBItem = TMDBItem()
var item: WatchlistItemViewModel
var body: some View {
DisplayGridItem(item: item, tmdbItem: tmdbItem)
.onAppear {
self.isLoading = true
movieLogicState.loadMovie(id: item.tmdbId) { result in
self.isLoading = false
self.tmdbItem = result
}
}
}
}
struct DisplayGridItem: View {
var item: WatchlistItemViewModel
var tmdbItem: TMDBItem = TMDBItem()
var body: some View {
if let releaseDate = tmdbItem.regionFinalReleaseDate {
if releaseDate > Date() {
Text("\(releaseDate) | \(Date())")
Text(tmdbItem.finalTitle)
}
}
}
}