如何解决如何在不使用onDeleteperform :)的情况下从子视图中删除SwiftUI中的列表项?
我的应用程序中具有聊天功能,可让您报告并阻止某人与您进行进一步的交流。
这是我的InboxView.swift
,显示用户的对话:
List(Array(conversations.conversations.enumerated()),id: \.1.id){ (index,conversation) in
VStack{
NavigationLink(destination: ChatView(conversation_id: conversation.id,avatar: conversation.avatar,displayName: conversation.displayName,user_id: conversation.receiver_id,parentIndex: index)){
ConversationList(id : conversation.id,user_id : conversation.user_id,receiver_id : conversation.receiver_id,lastMessage : conversation.lastMessage,avatar : conversation.avatar,displayName : conversation.displayName,startedAt : conversation.startedAt)
}
Divider()
}
}
上面的代码只是为最终用户提供了一个界面,供他们选择想要加入的对话。下面的视图图使事情变得棘手:
InboxView --> ChatView --> ProfileView
每个-->
代表一个NavigationLink
,它会导致后面的视图。在ProfileView.Swift
页面上,我提供了一个按钮,最终用户可以在其中阻止与之交谈的人。我已经弄清楚了如何通过一系列
InboxView
@Environment(\.presentationMode) var mode
和
self.mode.wrappedValue.dismiss()
但是为了方便起见,我也想删除与被阻止的用户对话关联的列表项。
如何知道InboxView
是哪个ChatView
触发了删除请求,并将其通过这样的函数传递?
func removeRow(at offsets: IndexSet){
if let first = offsets.first {
let conversationRemoving = conversations.conversations[first]
conversations.conversations.remove(at:first)
}
}
我没有在文档中看到presentationMode
通过wrappedValue
触发功能
解决方法
可以直接在List
内部完成操作(因为我们可以访问其中的索引),并从已获取的结果中删除记录。
如果人员模型具有特定字段(例如blocked
),则可能如下所示(用伪代码,简称):
List(Array(conversations.conversations.enumerated()),id: \.1.id){ (index,conversation) in
VStack{
NavigationLink(destination: ChatView(...)) {
ConversationList(...)
}
Divider()
}
.onAppear { // called on show and on return back
if conversation.receiver.blocked { // << here !!
// better to do it asynchronously
DispatchQueue.main.async {
self.conversations.conversations.remove(at: index) // << here !!
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。