刷新 SwiftUI 列表 [英] Refreshing a SwiftUI List

查看:72
本文介绍了刷新 SwiftUI 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我点击导航链接时,我都会尝试刷新此列表

Ím trying to refresh this List whenever I click on a 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 做到了)有什么想法吗?

The list is made out of an array inside an environment object. The problem: It does only refresh when I switch to another tab or close the app I had used a modal View before and it worked there. (I did it with .onAppear) Any Ideas?

示例

问题:当您点击列表中的一个项目并点击切换按钮时,EnvironmentObject 已更改,但仅当我更改选项卡并再次将其更改回来时才会反映此更改

Problem: When you tap on an item in the list and tap the toggle button the EnvironmentObject is changed but this changed is only reflected when I change the tab and change it back again

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)
        }
    }
}

推荐答案

我遇到了类似的问题,这是我想出的 hack.

I had a similar problem, this is the hack I came up with.

在您的TestView"中声明:

In your "TestView" declare:

@State var needRefresh: Bool = false

将此传递给您的detailView"目的地,例如:

Pass this to your "detailView" destination, such as:

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" 时强制刷新改变了.

Note ".accentColor(self.needRefresh ? .white : .black)" to force a refresh when "needRefresh" is changed.

在您的detailView"目的地添加:

In your "detailView" destination add:

@Binding var needRefresh: Bool

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

Then in your "detailView" in your Button action, add:

self.needRefresh.toggle()

这篇关于刷新 SwiftUI 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆