foreach - 如何在子结构中重新加载当前视图的数据
问题描述
我的应用是电影应用。有一个SingleMovieView
显示一部电影的相关信息,如导演,演员......在底部,它是一个滚动视图,显示类似的电影列表。当用户单击其中一项时,应导航到选定的电影视图。
好吧,问题实际上是SingleMovieView
目的地是一样的。或者说我想为所选电影重用这个视图和不同的数据。目前,我在 ForEach of 中使用导航链接RecMovieList
,并且录音不起作用。那么,如何在 SwiftUI 中对同一个 View 进行自调用呢?
这是代码:
import SwiftUI
import KingfisherSwiftUI
struct SingleMovieView: View {
var movieId: Int = -1
@ObservedObject var model = MovieListViewModel()
var body: some View {
ScrollView(showsIndicators: false) {
VStack(alignment: .leading) {
createPosterImage()
MovieDetailView(movie: self.model.movie)
if model.secSectionMoviesBundle.isEmpty {
Text("Loading")
} else {
VStack(alignment: .leading, spacing: 12) {
CrewList(crews: (model.secSectionMoviesBundle[.Crew] as! [CrewViewModel]).filter {$0.job == "Director"} )
CastList(casts: model.secSectionMoviesBundle[.Cast] as! [CastViewModel])
ImageList(images: model.secSectionMoviesBundle[.Images] as! [ImageViewModel])
RecMovieList(movies: model.secSectionMoviesBundle[.Recomm] as! [MovieViewModel])
}
}
}
}.edgesIgnoringSafeArea(.top)
.onAppear() {
self.model.getMovieDetail(id: self.movieId)
self.model.getSecSectionMoviesBundle(id: self.movieId)
}
}
// struct CrewList
// struct CastList
// struct ImageList
struct RecMovieList: View {
var movies: [MovieViewModel]
var body: some View {
VStack(alignment: .leading) {
Text("\(SecHomeSection.Recomm.rawValue)")
.font(.headline)
ScrollView(.horizontal) {
HStack(alignment: .top, spacing: 10) {
ForEach(0..<movies.count) { i in
VStack(alignment: .leading) {
NavigationLink(destination: SingleMovieView(movieId: self.movies[i].id)) {
KFImage(source: .network(self.movies[i].posterUrl))
.renderingMode(.original)
.resizable()
.frame(width: 100, height: 150)
.aspectRatio(2/3, contentMode: .fill)
}
Text("\(self.movies[i].title)")
.lineLimit(2)
.foregroundColor(.gray)
.frame(height: 50, alignment: .top)
}.frame(width: 100)
}
}
}
//.frame(height: 100)
}
.padding(.horizontal).padding(.bottom)
}
}
fileprivate func createPosterImage() -> some View {
return KFImage(source: .network(model.movie.posterUrl))
.resizable().aspectRatio(contentMode: .fit)
}
}
// Update:,作为评论,我意识到这是当前视图的重新加载数据而不是导航。所以我也在 struct RecMovieList 中添加了 ObservedObject,那么现在发布的模型也可以在这个 struct 中使用,在 onAppear() 中重做网络请求。
我将 NavigationLink 更改为 Button,因为不需要导航。好吧,它可以工作并且视图可以重新加载,但是当我在应用程序中点击电影列表时,它会触发触摸事件/重新加载数据,即使是拖动、滑动也是如此。我该如何解决这个问题?使用 TapGesture?
struct RecMovieList: View {
var movies: [MovieViewModel]
@ObservedObject var model = MovieListViewModel()
var body: some View {
VStack(alignment: .leading) {
Text("\(SecHomeSection.Recomm.rawValue)")
.font(.headline)
ScrollView(.horizontal) {
HStack(alignment: .top, spacing: 10) {
ForEach(0..<movies.count) { i in
VStack(alignment: .leading) {
Button(action: {
self.model.getMovieDetail(id: self.movies[i].id)
self.model.getSecSectionMoviesBundle(id: self.movies[i].id)
}) {
KFImage(source: .network(self.movies[i].posterUrl))
.renderingMode(.original)
.resizable()
.frame(width: 100, height: 150)
.aspectRatio(2/3, contentMode: .fill)
}
Text("\(self.movies[i].title)")
.lineLimit(2)
.foregroundColor(.gray)
.frame(height: 50, alignment: .top)
}.frame(width: 100)
}
}
}
//.frame(height: 100)
}
.padding(.horizontal).padding(.bottom)
}
}
解决方案
尝试使用 NavigationView{} 覆盖 navigationLink(){}
NavigationView {
NavigationLink(destination: DestinationView()) { // code }
}
如果它没有帮助,也尝试覆盖整个“身体”
var body: some View {
NavigationView {
//code
}
}
更新:
尝试使用与默认按钮样式不同的推荐按钮图像
.buttonStyle(BorderlessButtonStyle())
推荐阅读
- c++ - 是否可以将 SDL_Texture 绘制到 OpenGL?
- python - 如何根据另一个二维列表的长度值创建一个空的二维列表?
- excel-formula - XLSXWriter:=CONCAT() 的公式在 Excel 中显示为“=@CONCAT()”?
- r - DT::formatStyle 根据原始 HTML 字符向量设置数据表行的背景颜色
- asp.net-mvc - 如何在 Asp.Net MVC - Razor 视图中引用视图模型属性
- openpyxl - Openpyxl 没有抛出错误,但没有对工作簿进行更改?
- opengl - 在使用较大的工作组时,我遇到了非常慢的 OpenGL 计算着色器编译(10 分钟以上),有什么办法可以加快速度吗?
- if-statement - 如果我的 SSRS 报告中的其他代码不起作用
- vb.net - 为函数发生器创建程序
- latex - Pandoc 不使用乳胶在降价中生成新行