刷新SwiftUI列表

时间:2019-08-30 13:15:14

标签: swiftui swiftui-list

我试图在每次单击NavLink时刷新此列表

NavigationView {
    List(feed.items.indices, id:\.self) { i in
        NavigationLink(destination: ListFeedItemDetail(idx: i).environmentObject(self.feed)) {
            ListFeedItem(item: self.$feed.items[i])
        }
    }
}

该列表由环境对象内的数组组成。 问题:仅当我切换到另一个选项卡或关闭应用程序时,它才会刷新 我以前使用过模式视图,并且在那里工作。 (我使用.onAppear做到了) 有任何想法吗?

示例

问题:当您点击列表中的某个项目并点击切换按钮时,环境对象已更改,但是只有当我更改选项卡并再次将其更改回时,此更改才会反映出来

import SwiftUI
import Combine

struct TestView: View {

    @State var showSheet: Bool = false
    @EnvironmentObject var feed: TestObject
    func addObjects() {
        var strings = ["one","two","three","four","five","six"]
        for s in strings {
            var testItem = TestItem(text: s)
            self.feed.items.append(testItem)
        }
    }

    var body: some View {
        TabView {
            NavigationView {
                List(feed.items.indices, id:\.self) { i in
                    NavigationLink(destination: detailView(feed: self._feed, i: i)) {
                        HStack {
                            Text(self.feed.items[i].text)
                            Text("(\(self.feed.items[i].read.description))")
                        }
                    }
                }
            }
                .tabItem({ Text("Test") })
                .tag(0)

            Text("Blank")
                .tabItem({ Text("Test") })
                .tag(0)
        }.onAppear {
            self.addObjects()
        }
    }
}

struct detailView: View {
    @EnvironmentObject var feed: TestObject
    var i: Int
    var body: some View {
        VStack {
            Text(feed.items[i].text)
            Text(feed.items[i].read.description)
            Button(action: {  self.feed.items[self.i].isRead.toggle() }) {
                Text("Toggle read")
            }
        }
    }
}

final class TestItem: ObservableObject {
    init(text: String) {
        self.text = text
        self.isRead = false
    }
    static func == (lhs: TestItem, rhs: TestItem) -> Bool {
        lhs.text < rhs.text
    }
    var text: String
    var isRead: Bool

    let willChange = PassthroughSubject<TestItem, Never>()
    var read: Bool {
        set {
            self.isRead = newValue
        }
        get {
            self.isRead
        }
    }
}

class TestObject: ObservableObject {
    var willChange = PassthroughSubject<TestObject, Never>()

    @Published var items: [TestItem] = [] {
        didSet {
            willChange.send(self)
        }
    }
}

1 个答案:

答案 0 :(得分:3)

我有一个类似的问题,这是我想出的一个方法。

在您的“ TestView”中声明:

@State var needRefresh: Bool = false

将此传递到您的“ detailView”目的地,例如:

NavigationLink(destination: detailView(feed: self._feed, i: i, needRefresh: self.$needRefresh)) {
     HStack {
            Text(self.feed.items[i].text)
            Text("(\(self.feed.items[i].read.description))")
     }.accentColor(self.needRefresh ? .white : .black)
 }

注意“ .accentColor(self.needRefresh?.white:.black)”在“ needRefresh”时强制刷新 改变了。

在“ detailView”目标位置中添加:

@Binding var needRefresh: Bool

然后在“按钮”操作的“ detailView”中添加:

self.needRefresh.toggle()