如何在子结构中重新加载当前视图的数据

时间:2020-04-07 14:52:19

标签: foreach navigation swiftui

我的应用是电影应用。有SingleMovieView用于展示一部电影的相关信息,例如 导演,演员...在底部,它是一个滚动视图,显示相似的电影列表。当用户单击一项时,应导航到所选的电影视图。

好吧,问题在于目的地实际上是SingleMovieView。或者说我想为选定的电影对不同的数据重复使用此视图。当前,我正在RecMovieList的ForEach中使用导航链接,并且编带不起作用。那么,如何在SwiftUI中对同一View进行自调用?

enter image description here

以下是代码:

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)
    }
}

//更新:,作为注释,我意识到这是当前视图的重新加载数据,而不是导航。因此,我也将ObservedObject添加到结构RecMovieList中,然后现在也可以在该结构中使用已发布的模型,以在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)
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试用NavigationView {}覆盖navigationLink(){}

NavigationView {
  NavigationLink(destination: DestinationView()) { // code }
}

如果没有帮助,请尝试覆盖整个“身体”

 var body: some View { 
    NavigationView {
    //code
    }   
 }

更新:

尝试为您推荐按钮图像使用不同于默认按钮样式的

answer

.buttonStyle(BorderlessButtonStyle())