簡體   English   中英

SwiftUI:將動態數據傳遞給內容視圖后重繪

[英]SwiftUI: Redraw after passing dynamic data to content view

我有類似下面的內容,除了我在“ChildContentView”中有額外的動態手勢。

我將如何強制子視圖重繪/重新加載? 目前,任何動態內容繪制一次並且不會重新加載。 所有內容本質上都是靜態的。

更新:需要使用let content: () -> Content而不是@State var content: () -> Content

// ChildContentView
struct ChildContentView<Content: View>: View {
    @State var content: () -> Content

    var body: some View {
        let dragGesture = DragGesture()...

        return Group {
            self.content()
        }.gesture(dragGesture)
    }
}
// ContentView
...
@State var changingList: [FooItem] = [] <-- This is dynamically changed.  Not reflected in this code snippet.

var body: some View {
    ZStack { 
        ChildContentView {
            List(changingList) { item in
                print("\(item)")
            }
        }
    }
}
...

你的麻煩是 State 屬性包裝器

struct ChildContentView<Content: View>: View {

@State var content: () -> Content

    var body: some View {
        let dragGesture = DragGesture()...

        return Group {
            self.content()
        }.gesture(dragGesture)
    }
}

它使內容“靜態”(實際上你做到了:-))

讓我們看看並嘗試這個例子(復制 - 粘貼 - 在你的模擬器中運行)

import SwiftUI
struct Child<Content: View>: View {
    var content: () -> Content
    var body: some View {
        content()
    }
}

struct Item: Identifiable {
    let txt: String
    let id = UUID()
}

struct ContentView: View {
    @State var items = [Item(txt: "Alfa"), Item(txt: "Beta")]
    var body: some View {
        ZStack {
            Child {
                List(self.items) { (item) in
                    Text(item.txt)
                }
            }
            Button(action: {
                self.items.append(contentsOf: [Item(txt: "game"), Item(txt: "Delta")])
            }) {
                Text("Tap me to change items").padding().padding(.vertical, 50).background(Color.yellow)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

確保列表中的項目在內容更改時修改其id值。 基本上散列視覺內容以創建每個視覺狀態的唯一值。

例如

struct FooItem: Identifiable {
  var id: UUID = UUID()
  var text: String = ""

  var visualID: String {
    return id.uuidString
      + "\(text.hashValue)"
  }

}

我猜當id沒有改變時,內部ZStack緩存無法識別對象已經改變。

所以使用ForEach因為這就是我在我的代碼中得到的(但List也有id )......

var body: some View {
    ZStack {
      ForEach(self.changingList, id: \.visualID) { item in
        SomeView(item: item)
          .onTapGesture {
            //blah blah
        }
      }
    }
}

我在這個確切的問題上猶豫了好幾天,直到解決為止。

我剛接觸 SwiftUI 一個月,所以我不完全理解為什么會這樣。 但是,將變量設置為常量“讓”直接通過更新傳遞視圖內容。

更改自:

struct ChildContentView<Content: View>: View {
    @State var content: () -> Content

    var body: some View {
        let dragGesture = DragGesture()...

        return Group {
            self.content()
        }.gesture(dragGesture)
    }
}

對此:

struct ChildContentView<Content: View>: View {
    let content: () -> Content

    var body: some View {
        let dragGesture = DragGesture()...

        return Group {
            self.content()
        }.gesture(dragGesture)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM