删除项目时出现问题。 并且只有当我们从末尾(当前索引 4)或从开头(当前索引 0)打开项目列表并立即(不滚动)删除当前项目。 在这种情况下,TabView 不会更改当前元素(选择:$current)并且 onChange 事件不会触发。
有谁知道我做错了什么还是 TabView 错误?
import SwiftUI
import RealmSwift
final class Album: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var items = RealmSwift.List<Item>()
}
final class Item: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted(originProperty: "items") var album: LinkingObjects<Album>
@Persisted var label: String
}
func genAlbum () -> Album {
let realm = try! Realm()
let album = Album()
try! realm.write {
album.items.append(Item(value: ["label": "0"]))
album.items.append(Item(value: ["label": "1"]))
album.items.append(Item(value: ["label": "2"]))
album.items.append(Item(value: ["label": "3"]))
album.items.append(Item(value: ["label": "4"]))
realm.add(album)
}
print(album)
return album
}
let albumStore = genAlbum()
struct ContentView: View {
@ObservedRealmObject var album = albumStore
@State private var selectedItem: Item?
var body: some View {
VStack {
Text("Count: \(self.album.items.count)")
HStack (spacing: 10) {
ForEach(self.album.items, id: \._id) { item in
Button(action: { self.selectedItem = item },
label: { Text("\(item.label)")
.font(.largeTitle)
.tag(item)
}
)
.buttonStyle(.bordered)
.fullScreenCover(item: self.$selectedItem, content: {
currentItem in
AlbumView(album: self.album, current: currentItem, onDismiss: { self.selectedItem = nil })
.background(.blue)
.padding()
})
}
}
}
}
}
struct AlbumView: View {
@ObservedRealmObject var album: Album
@State var current: Item
var onDismiss: (() -> Void)? = nil
var body: some View {
TabView(selection: $current) {
ForEach(self.album.items, id: \._id) { item in
Text(item.label)
.font(.largeTitle)
.tag(item)
.background(.red)
.padding()
}
}
.tabViewStyle(.page(indexDisplayMode: .never))
.onChange(of: current) { oldCurrent, newCurrent in
print("::::onChange current", oldCurrent.label, newCurrent.label)
}
.overlay(
VStack {
Spacer()
HStack {
let i = album.items.index(of: current)
Text ("overlay tab current item :\(current.label) \(i ?? -1)")
if let index = album.items.index(of: current) {
Button(action: {
print("Delete \(index)")
$album.items.remove(at: index)
print("Items count \($album.items.count)")
print("Indexes \($album.items.indices)")
},
label: {
Text("Delete label:\(current.label) index \(index)") }
).padding().background(.red)
}
Spacer()
Button(action: {
if let dismiss = self.onDismiss {
dismiss()
} }, label: { Text("Close") })
.padding().background(.red)
}
}
)
}
}
在
AlbumView
:
@State var current: Item
应改为:
@Binding var current: Item