首页 > 解决方案 > 警报接受时的 SwiftUI 刷新列表

问题描述

一旦删除了 SwiftUI 列表中的刷新列表项,我在 SwiftUI 实现中遇到问题。

该列表代表一个用户的好友请求,每一行都是一个视图的唯一实例FriendRequestRow()

这是一张图片,显示了朋友请求列表中每一行的样子https://i.stack.imgur.com/oPPDI.jpg

我的问题是,当单击红色 X 按钮并将用户带到警报(https://i.stack.imgur.com/yAoqH.jpg)并选择“删除”时,它成功地从应用程序数据库中删除了该值,但删除的行会保留在列表中,直到重新呈现视图。

下面是保存列表的视图的代码,删除了一些非必要的行。

struct FriendsListView: View {

@State var Friend_Request_Array : [Friend] = []
@State var Current_Friend_Array : [Friend] = []

var body: some View {
    VStack {
        HStack{
            Text("Friend Requests")
            .font(.headline)
            .padding()
            .foregroundColor(Color.white)

            Spacer()
        }
        .background(Color(UIColor.systemGray))
        .padding(.top, -8)

        // List of friend requests.
        List {
            ForEach(Friend_Request_Array) { Friend in
                FriendRequestRow(Friend: Friend, Friend_Display_Name: "")
            }
        }
        .buttonStyle(PlainButtonStyle())
        .frame(minHeight: 0, maxHeight: 150)

    }.navigationBarTitle(Text("Your Friends"), displayMode: .inline)
    .navigationBarItems(trailing: Button(action: {

    }) {
        Image(systemName: "plus")
    })
        .onAppear{
            self.Friend_Request_Array = []
            self.Current_Friend_Array = []
            self.Load_Friends()
    }
}

func Load_Friends() {
    // Load lists full of friends.

    let db = Firestore.firestore()

    // Populates friend request array with pending friend requests.

    // Populates current friends array with all of the current users' friends.
}

}

然后,这是每个单独的列表行的视图:

struct FriendRequestRow: View {

@State var Friend: Friend
@State var Friend_Display_Name: String

@State private var showingDeleteRequestAlert = false

var body: some View {
    HStack{
        Image("example-avatar")
            .resizable()
            .frame(width: 40, height: 40)
            .clipShape(Circle())

        VStack(alignment: .leading){
            Text(Friend_Display_Name)
                .font(.headline)
        }.padding()

        Spacer()

        Button(action: {
            // Accept request.

        }) {
            Image(systemName: "checkmark.circle.fill")
                .font(.system(size: 30))
                .foregroundColor(Color(UIColor.systemGreen))
        }


        Button(action: {
            // Deny and remove friend request. Should pop up an 'are you sure' alert.
            self.showingDeleteRequestAlert = true
        }) {
            Image(systemName: "xmark.circle.fill")
                .font(.system(size: 30))
                .foregroundColor(Color(UIColor.systemRed))
        }
        .alert(isPresented: $showingDeleteRequestAlert) {
        Alert(title: Text("Are you sure?"),
              message: Text("If you delete this request it will have to be resent."), primaryButton: .destructive(Text("Remove")) {
                // Removal of request from the database

                // Re-render list
                // **THIS IS WHERE MY ISSUE IS, HOW DO I RERENDER LIST?**


            }, secondaryButton: .cancel())
        }


    }.onAppear{
        // Get Each users display name.
    }
}

如图所示,当从导航链接加载视图时,我使用 onAppear 属性填充列表。但是,这意味着当用户单击“删除”每个好友请求时,它不会反映在列表中,直到重新呈现视图(单击返回,然后再次单击导航链接)。帮助!

标签: iosswiftfirebasegoogle-cloud-firestoreswiftui

解决方案


推荐阅读