SwiftUI TabView 和 Realm:删除第一个和最后一个元素不起作用

问题描述 投票:0回答:1
  1. 我们有一个领域数据库:Album which Items。
  2. 我们在 TabView 中打开相册并尝试滚动项目并删除每个项目。此外,我们可以在 TabView 中使用任何当前项目打开相册。例如,如果我们单击 3,我们将打开 TabView,其中当前项目将为 3。 效果很好。

删除项目时出现问题。 并且只有当我们从末尾(当前索引 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)

                }
            }
        )

    }
}

swiftui realm tabview
1个回答
0
投票

AlbumView

@State var current: Item

应改为:

@Binding var current: Item
© www.soinside.com 2019 - 2024. All rights reserved.