Refreshing a SwiftUI List

前端 未结 1 1608
甜味超标
甜味超标 2020-12-19 02:23

Ím trying to refresh this List whenever I click on a NavLink

NavigationView {
    List(feed.items.indices, id:\\.self) { i in
        NavigationLink(destinat         


        
相关标签:
1条回答
  • 2020-12-19 02:58

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

    In your "TestView" declare:

    @State var needRefresh: Bool = false
    

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

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

    In your "detailView" destination add:

    @Binding var needRefresh: Bool
    

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

    self.needRefresh.toggle()
    
    0 讨论(0)
提交回复
热议问题