swift - Swift UI 细节移除
问题描述
我有一个 SwiftUI 列表,它在点击单元格时显示详细视图/推送到导航:
import SwiftUI
struct DevicesInRangeList: View {
@ObservedObject var central = Central()
var body: some View {
NavigationView {
List(central.peripheralsInRange) { peripheral in
NavigationLink(destination: DeviceView(peripheral: peripheral).onAppear {
self.central.connect(peripheral: peripheral)
}.onDisappear {
self.central.disconnect(peripheral: peripheral)
}) {
DeviceRow(deviceID: peripheral.deviceID, name: peripheral.name)
}
}.onAppear {
self.central.scanning = true
}.onDisappear {
self.central.scanning = false
}.navigationBarTitle("Devices in range")
}
}
}
如果我点击一行,则会显示详细信息。如果外围设备断开连接,它将从 peripheralsInRange 数组中删除,并且该行被删除——但仍会显示详细信息。删除关联行时如何删除详细信息?
编辑:在 Asperi 的回答之后,我有以下内容,但仍然无法正常工作:
struct DevicesInRangeList: View {
@ObservedObject var central = Central()
@State private var localPeripherals: [Peripheral] = []
@State private var activeDetails = false
var body: some View {
NavigationView {
List(localPeripherals, id: \.self) { peripheral in
NavigationLink(destination:
DeviceView(peripheral: peripheral)
.onReceive(self.central.$peripheralsInRange) { peripherals in
if !peripherals.contains(peripheral) {
self.activeDetails = false
}
}
.onAppear {
self.central.connect(peripheral: peripheral)
}
.onDisappear {
self.central.disconnect(peripheral: peripheral)
}
, isActive: self.$activeDetails) {
DeviceRow(deviceID: peripheral.deviceID, name: peripheral.name)
}
}.onReceive(central.$peripheralsInRange) { peripherals in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.localPeripherals = peripherals
}
}.onAppear {
self.central.scanning = true
self.localPeripherals = self.central.peripheralsInRange
}.onDisappear {
self.central.scanning = false
}.navigationBarTitle("Devices in range")
}
}
}
解决方案
最好的方法是在显示之前检查是否存在 od 数据。我采用了苹果的大师/演示来展示如何做到这一点。在这个模板应用程序中,他们使用@State var 作为记录源,但想法是一样的。在详细视图中检查“记录”是否存在。
import SwiftUI
private let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .medium
return dateFormatter
}()
struct ContentView: View {
@State private var dates = [Date]()
var body: some View {
NavigationView {
MasterView(dates: $dates)
.navigationBarTitle(Text("Master"))
.navigationBarItems(
leading: EditButton(),
trailing: Button(
action: {
withAnimation { self.dates.insert(Date(), at: 0) }
}
) {
Image(systemName: "plus")
}
)
DetailView(dates: $dates).navigationBarTitle(Text("Detail"))
}.navigationViewStyle(DoubleColumnNavigationViewStyle())
}
}
struct MasterView: View {
@Binding var dates: [Date]
var body: some View {
List {
ForEach(dates, id: \.self) { date in
NavigationLink(
destination: DetailView(dates: self._dates, selectedDate: date).navigationBarTitle(Text("Detail"))
) {
Text("\(date, formatter: dateFormatter)")
}
}.onDelete { indices in
indices.forEach { self.dates.remove(at: $0) }
}
}
}
}
struct DetailView: View {
@Binding var dates: [Date]
var selectedDate: Date?
var body: some View {
if let selectedDate = selectedDate, dates.contains(selectedDate) {
return Text("\(selectedDate, formatter: dateFormatter)")
} else {
return Text("Detail view content goes here")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
推荐阅读
- openshift - 如何将秘密链接到 OpenShift 模板中的默认管道 ServiceAccount?
- javascript - 为什么即使将超时设置为0,回调函数仍然延迟
- safari - 无法在 Safari 上设置远程视频描述发送参数,但在 Chrome 和 Firefox 中运行良好
- ios - 如何创建与已初始化的枚举一起使用的协议扩展?
- cuda - 在 GPU 上使用两个以上列表生成笛卡尔积
- excel - Excel宏需要很长时间
- python - 将字符串转换为类变量名
- forms - 在 ChipBuilder 中显示建议列表
- python - Rllib 离线 SAC 的数据准备
- jmeter - 对 quasar 项目的 UI 建议进行负载测试