视图的 init 在 tabview SwiftUI 中只调用一次

时间:2021-07-01 03:38:51

标签: swiftui

我正在制作一个应用程序,我正在为当前用户更新 profileView 选项卡中的个人资料图片。然后我导航到 feedView 选项卡,我希望在与当前用户相关的每个帖子中更改个人资料图片。但是在 feedview 中的个人资料图片没有更新。如果我重新运行应用程序,我会正确地看到提要视图中的更改。

于是我调试了应用程序,发现tabview中FeedCellview的init()只是第一次被调用。当我使用应用程序并导航到 Feedview 时,没有调用 init,因此我无法从视图模型更新数据。

主视图:

<!DOCTYPE html>
<html lang="en">

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>

<body>
    <form method="post" action="">
        <div class="row">
            <div class="col-lg-12">
                <div id="inputFormRow">
                    <div class="input-group mb-3">
                        <input type="text" name="title[]" class="form-control m-input" placeholder="Enter title"
                            autocomplete="off">
                        <div class="input-group-append">
                            <button id="removeRow" type="button" class="btn btn-danger">Remove</button>
                        </div>
                    </div>
                </div>
                <div id="newRow"></div>
                <button id="addRow" type="button" class="btn btn-info">Add Row</button>
            </div>
        </div>
    </form>


    <br><br>
    <div id="face-two">
        <hr>
        <h1> I am Row 1</h1>
    </div>

</body>
</html>

供稿视图:

var body: some View {
        TabView (selection: $tabSelection){
            NavigationView{
                FeedView()
                    .navigationTitle("Feed")
                    .navigationBarItems(trailing: logoutButton)
            }
            .tabItem {
                Image(systemName: "house")
            }.tag(1)
            ...................
            ...................

FeedCellView:

import SwiftUI

struct FeedView: View {
    @StateObject private var feedViewModel = FeedViewModel()
    var body: some View {
        ScrollView(){
            LazyVStack(spacing:32){
                ForEach(feedViewModel.posts){post in
                    FeedCellView(viewModel: FeedCellViewModel(post: post))
                }
            }
        }
    }
}

FeedCellViewModel:

struct FeedCellView: View {
    @ObservedObject var feedCellViewModel:FeedCellViewModel
    
    init(viewModel:FeedCellViewModel){
        self.feedCellViewModel = viewModel //This init is not being called after first time
        print("feed Cell View Init")
    }
    
    var body: some View {
        VStack(alignment:.leading){
            
            if let user = feedCellViewModel.post.user{
                NavigationLink(
                    destination: ProfileView(user: user),
                    label: {
                        HStack{
                            if let imageUrl = feedCellViewModel.post.ownerImageUrl{
                                KFImage(URL(string: imageUrl))
                                    .resizable()
                                    .scaledToFill()
                                    .frame(width:36,height: 36)
                                    .clipped()
                                    .cornerRadius(18)
                            }else{
                                Image("profile-placeholder")
                                    .resizable()
                                    .scaledToFill()
                                    .frame(width:36,height: 36)
                                    .clipped()
                                    .cornerRadius(18)
                            }
................
................
 

为什么 class FeedCellViewModel:ObservableObject{ @Published var post:PostModel init(post:PostModel){ self.post = post fetchUser() checkLike() } func fetchUser(){ print("Feed cell ViewModel fetchUser called") Firestore.firestore().collection("users").document(post.ownerId).getDocument { (snap, error) in if let error = error{ print(error.localizedDescription) return } DispatchQueue.main.async { self.post.user = try? snap?.data(as: UserModel.self) guard let userImageUrl = self.post.user?.profileImageUrl else{return} self.post.ownerImageUrl = userImageUrl print("OwnerImageUrl \(self.post.ownerUsername): \(self.post.ownerImageUrl ?? "No Image Url")") } } } ............................ ............................ 在 FeedCellVIew 中只调用一次?我使用的是 Xcode 12.2。

0 个答案:

没有答案