ios - 警报接受时的 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 属性填充列表。但是,这意味着当用户单击“删除”每个好友请求时,它不会反映在列表中,直到重新呈现视图(单击返回,然后再次单击导航链接)。帮助!
解决方案
推荐阅读
- sql - SQL - 使用部分字符串排序
- mysql - 如何按条件排序?
- html - 如何在 DataTable 分页中添加“上一个”和“下一个”按钮以及“列表框”
- r - R:如何使networkD3网络对数据的子集做出反应
- vba - 如何引用 VBA 中的字段并从其他字段确定它们的值?
- python - 如何在 python 中合并大型 csv 文件?
- spring-boot - 无法使用 Spring Batch + Spring 数据在数据库中插入行
- java - 从其他类填充数组时出现未识别的 NullPointerException
- bash - 在 bash 中替代 Ctrl-R 反向搜索
- html - 将 chrome 缩放到 125 或其他比例时,图标对齐不好?